How to check if-statement in method using Mockito and JUnit?(如何使用 Mockito 和 JUnit 检查方法中的 if 语句?)
问题描述
我有我应该测试的方法.代码(当然有些部分被剪掉了):
I have method that I should test. Code (of course some parts were cut):
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 语句是否正常工作.
Question: how to check if-statement in method getPossibleFilterData using Mockito and JUnit? I want pass equal dates to method then check that my if-statement works properly.
推荐答案
如果你真的想要一个 纯 单元测试而不是集成测试,你可以依赖注解 @Mock 来模拟你的服务 FilterDataProvider 和 @InjectMocks 来将你的模拟注入到你的 FilterDataController 实例中.
If you really want a pure unit test not an integration test, you could rely on the annotation @Mock to mock your service FilterDataProvider and @InjectMocks to inject your mock into your instance of FilterDataController.
那么你可以提出 3 个测试:
- 一个日期正确但不同的测试,
- 另一个日期正确但相等的日期
- 最后一个日期不正确会抛出
ValueNotAllowedException,可以使用@Test(expected = ValueNotAllowedException.class)进行测试.
- One test where the dates are corrects but different,
- Another one where the dates are corrects but equal
- And the last one where the dates are incorrect which will thrown a
ValueNotAllowedExceptionthat could be tested out of the box using@Test(expected = ValueNotAllowedException.class).
如果您需要确保 filterDataProvider.getPossibleCountries(startDate, newEndDate) 已使用您需要使用的预期参数调用 verify.
If you need to make sure that filterDataProvider.getPossibleCountries(startDate, newEndDate) has been called with the expected arguments you need to use 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
}
}
这篇关于如何使用 Mockito 和 JUnit 检查方法中的 if 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Mockito 和 JUnit 检查方法中的 if 语句?
基础教程推荐
- Java Swing计时器未清除 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
