我有我应该测试的方法。代码(当然有些部分被删除):
public class FilterDataController {
public static final String DATE_FORMAT = "yyyy-MM-dd";
@Autowired
private FilterDataProvider filterDataProvider;
@ApiOperation(value = "Get possible filter data",response = ResponseEntity.class)
@ApiResponses(value = {
@ApiResponse(...),
@ApiResponse(...)})
@RequestMapping(path = "...", method = RequestMethod.GET)
public ResponseEntity<Object> getPossibleFilterData(
@RequestParam(value = "startDate") @DateTimeFormat(pattern=DATE_FORMAT) final Date startDate,
@RequestParam(value = "endDate") @DateTimeFormat(pattern=DATE_FORMAT) final Date endDate) {
if (endDate.compareTo(startDate) == -1){
throw new ValueNotAllowedException("End date should be after or equal start date");
}
else {
Date newEndDate = endDate;
if (startDate.equals(endDate)){
newEndDate = new Date(endDate.getTime() + TimeUnit.DAYS.toMillis(1) - 1);
}
List<String> possibleCountries = Lists.newArrayList(filterDataProvider.getPossibleCountries(startDate, newEndDate));
return new ResponseEntity<>(new FilterResponse(possibleCountries),HttpStatus.OK);
}
}
}
问题:如何使用Mockito和JUnit检查方法getPossibleFilterData
中的if语句?我希望将方法传递给方法,然后检查我的if语句是否正常工作。
答案 0 :(得分:2)
如果你真的想要一个纯单元测试而不是集成测试,你可以依靠注释@Mock
模拟你的服务FilterDataProvider
和@InjectMocks
来将模拟注入您的FilterDataController
实例。
然后你可以提出3个测试:
ValueNotAllowedException
,可以使用@Test(expected = ValueNotAllowedException.class)
开箱即用。 如果您需要确保使用预期参数调用filterDataProvider.getPossibleCountries(startDate, newEndDate)
,则需要使用verify
。
代码就是这样的:
@RunWith(MockitoJUnitRunner.class)
public class FilterDataControllerTest {
@Mock
FilterDataProvider filterDataProvider;
@InjectMocks
FilterDataController controller;
@Test(expected = ValueNotAllowedException.class)
public void testGetPossibleFilterDataIncorrectDates() {
controller.getPossibleFilterData(new Date(1L), new Date(0L));
}
@Test
public void testGetPossibleFilterDataCorrectDates() {
// Make the mock returns a list of fake possibilities
Mockito.when(
filterDataProvider.getPossibleCountries(
Mockito.anyObject(), Mockito.anyObject()
)
).thenReturn(Arrays.asList("foo", "bar"));
ResponseEntity<Object> response = controller.getPossibleFilterData(
new Date(0L), new Date(1L)
);
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
// Make sure that
// filterDataProvider.getPossibleCountries(new Date(0L), new Date(1L))
// has been called as expected
Mockito.verify(filterDataProvider).getPossibleCountries(
new Date(0L), new Date(1L)
);
// Test response.getBody() here
}
@Test
public void testGetPossibleFilterDataEqualDates() {
// Make the mock returns a list of fake possibilities
Mockito.when(
filterDataProvider.getPossibleCountries(
Mockito.anyObject(), Mockito.anyObject()
)
).thenReturn(Arrays.asList("foo", "bar"));
// Call the controller with the same dates
ResponseEntity<Object> response = controller.getPossibleFilterData(
new Date(1L), new Date(1L)
);
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
Mockito.verify(filterDataProvider).getPossibleCountries(
new Date(1L), new Date(TimeUnit.DAYS.toMillis(1))
);
// Test response.getBody() here
}
}
答案 1 :(得分:1)
您必须模拟FilterDataProvider
,然后使用InjectMocks将其注入测试类。
getPossibleFilterData
将是测试中的方法,因此请选择任何特定日期(使用Calendar.set(...)
,然后Calendar.getTime()
)并将同一日期作为startDate和endDate发送。
现在getPossibleFilterData
完成后,您可以验证是否使用比开始日期多一个毫秒的结束日期调用了filterDataProvider.getPossibleCountries
。这可以通过模拟类方法中的Calendar.getTimeInMillis()
来完成,或者通过使用比最初指定的日期多一毫秒的日期来验证Mockito。
编辑:提供的代码示例:
public class FilterDataControllerTest {
@Test
public void testSameDate() {
FilterDataProvider provider = Mockito.mock(FilterDataProvider.class);
FilterDataController controller = new FilterDataController(provider);
Date startDate = new GregorianCalendar(2016, Calendar.JANUARY, 11).getTime();
Date endDate = new GregorianCalendar(2016, Calendar.JANUARY, 11).getTime();
Date expectedEndDate = new Date(endDate.getTime() + TimeUnit.DAYS.toMillis(1) - 1);
controller.getPossibleFilterData(startDate, endDate);
Mockito.verify(provider).getPossibleCountries(Mockito.eq(startDate), Mockito.eq(expectedEndDate));
}
}
答案 2 :(得分:0)
我看到两种主要方法。