【发布时间】:2016-12-05 11:18:11
【问题描述】:
我正在使用遗留代码,并希望增加它的测试覆盖率。
我有这样的课:
public class MyStrategy implements SomeStrategy {
TimeAndDateUtils timeAndDateUtils = new TimeAndDateUtils();
@Override
public boolean shouldBeExtracted(Argument argument) {
Date currentDate = timeAndDateUtils.getCurrentDate();
return currentDate.isBefore(argument.getDate());
}
}
我想测试 shouldBeExtracted-Method 并模拟对 timeAndDateUtils.getCurrentDate() 的调用,以便它返回一些固定值。
所以我想做的是:
Date currentDate = %some fixed date%
TimeAndDateUtils timeAndDateUtils = Mockito.mock(TimeAndDateUtils.class);
Mockito.when(timeAndDateUtils.getCurrentDate()).thenReturn(currentDate);
Assert.assertTrue(myStrategy.shouldBeExtracted(argument))
如何强制 MyStrategy-Class 使用模拟对象而不是创建自己的对象?
【问题讨论】:
标签: java unit-testing mocking mockito