public class DivisionCalculator {
private final Validator validator;
private final MathProvider mathProvider;
private final ViewProvider viewProvider;
public DivisionCalculator(Validator validator, MathProvider mathProvider, ViewProvider viewProvider) {
this.validator = validator;
this.mathProvider = mathProvider;
this.viewProvider = viewProvider;
}
public String calculate(int dividend, int divisor) {
validator.validate(dividend, divisor);
List<DivisionStep> items = mathProvider.calculate(dividend, divisor);
DivisionResult result = DivisionResult.builder()
.dividend(dividend)
.divisor(divisor)
.finalResult(mathProvider.finalResult(items))
.items(items)
.build();
return viewProvider.provideView(result);
}
}
public class DivisionCalculatorTest {
private ViewProvider viewProvider = Mockito.mock(ViewProvider.class);
private MathProvider mathProvider = Mockito.mock(MathProvider.class);
private Validator validator = Mockito.mock(Validator.class);
private DivisionCalculator divisionCalculator = new DivisionCalculator(validator, mathProvider, viewProvider);
DivisionResult makeDivision(int dividend, int divisor) {
List<DivisionStep> items = mathProvider.calculate(dividend, divisor);
return DivisionResult.builder()
.dividend(dividend)
.divisor(divisor)
.finalResult(mathProvider.finalResult(items))
.items(items)
.build();
}
@Test
void divisionCalculatorShouldSuccessfullyCallHisComponents() {
divisionCalculator.calculate(4, 2);
Mockito.verify(validator).validate(Mockito.anyInt(), Mockito.anyInt());
Mockito.verify(mathProvider).calculate(Mockito.anyInt(),Mockito.anyInt());
Mockito.verify(viewProvider).provideView(refEq(makeDivision(4, 2)));
}
}
public class DivisionTest {
@Mock
private Validator validator = new Validator();
@Mock
private ViewProvider viewProvider = new ViewProvider();
@Mock
private MathProvider mathProvider = new MathProvider();
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
}
@InjectMocks
private DivisionCalculator divisionCalculator = new DivisionCalculator(validator, mathProvider, viewProvider);
DivisionResult makeDivision(int dividend, int divisor) {
List<DivisionStep> items = mathProvider.calculate(dividend, divisor);
return DivisionResult.builder()
.dividend(dividend)
.divisor(divisor)
.finalResult(mathProvider.finalResult(items))
.items(items)
.build();
}
@Test
void divisionCalculatorShouldSuccessfullyCallHisComponents() {
divisionCalculator.calculate(4, 2);
Mockito.verify(validator).validate(4, 2);
Mockito.verify(mathProvider).calculate(4, 2);
Mockito.verify(viewProvider).provideView(refEq(makeDivision(4, 2)));
}
}
在执行以下步骤后,我得到了错误:想要但没有调用:实际上,与这个模拟没有任何交互。我不明白我在这里遗漏了什么。
如果有像@mock
和@injectmocks
这样的注释,则需要在JUnit5中使用一个特殊的扩展来运行Junit测试:
@ExtendWith(MockitoExtension.class)
class MyTest {
@Mock
private Validator validator;
...
}
还要注意,这个扩展为您创建模拟,并将值注入到带注释的数据字段中。因此,从技术上来说,创建像问题中所示的真实对象是错误的:
public class DivisionTest {
@Mock
private Validator validator = new Validator(); // this is wrong, don't use "new"
@Mock
private ViewProvider viewProvider = new ViewProvider(); // this is wrong, don't use "new"
@Mock
private MathProvider mathProvider = new MathProvider(); // this is wrong, don't use "new"
您可以按照本教程进行更深入的示例和解释。
@RunWith(MockitoJUnitRunner.class)
public class DivisionTest {...}
更新下面是异常消息: 更新2用真实实例替换mocked WithDefinitions对象后,我得到以下输出:
我有接口 接口的实现是 我还有一节课 我正在尝试为MyClass编写JUnit。我已经做了 但我需要mockito但没有调用,实际上在验证调用时与这个mock没有任何交互。 谁能提出一些解决方案。
我尝试了例外情况下给出的解决方案:mockito想要但没有调用,实际上与这个mock没有任何交互,而这个mockito也想要但没有调用:实际上,与这个mock没有任何交互但仍然得到相同的错误。我是不是漏掉了什么?以下是me的实现:-
一开始我想对我的英语感到抱歉。 这是我测试后得到的: 有人能告诉我我做错了什么吗?
MyDriveRepository.getMyDriveItemSelectedPathuri一直返回null。我试着查看以下链接,但没有找到解决问题的方法。
下面是我要测试的代码类 我的测试类