@Inject
private Accessor accessor;
@Dependent
public class DataReader {
String getData(String key) {
String value = accessor.getByDate(key, new Date());
return value;
}
}
public class DataReaderTest {
@Mock
Accessor accessor;
@InjectMocks @Spy
DataReader dataReader;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void test() {
when(accessor.getByDate(key, new Date())).thenReturn(now);
assertThat(dataReader.getData("key0"), is("todayData"));
}
}
when (accessor.getByDate (key, new Date ())). ThenReturn (now);
est en fait appelé ʻaccessor.getByDate (key, getNow ()) `, la valeur de l'argument de type Date change. J'ai échoué parce que j'ai fini@Inject
private Accessor accessor;
@Dependent
public class DataReader {
String getData(String key) {
String value = accessor.getByDate(key, getNow());
return value;
}
Date getNow() {
return new Date();
}
}
public class DataReaderReaderTest {
@Mock
Accessor accessor;
@InjectMocks @Spy
DataReader dataReader;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void test() {
Date now = new Date();
//C'est aussi bien
//when(deviceOptionReader.getNow()).thenReturn(now);
doReturn(now).when(deviceOptionReader).getNow();
when(accessor.getByDate(key, now)).thenReturn(now);
assertThat(dataReader.getData("key0"), is("todayData"));
}
}
N'écrivez pas de code qui crée un nouveau type Date directement dans l'argument de la méthode
Recommended Posts