我有接口
Interface MyInterface {
myMethodToBeVerified (String, String);
}
接口的实现是
class MyClassToBeTested implements MyInterface {
myMethodToBeVerified(String, String) {
…….
}
}
我还有一个班级
class MyClass {
MyInterface myObj = new MyClassToBeTested();
public void abc(){
myObj.myMethodToBeVerified (new String(“a”), new String(“b”));
}
}
我正在尝试为 MyClass 编写 JUnit。我已经做好了
class MyClassTest {
MyClass myClass = new MyClass();
@Mock
MyInterface myInterface;
testAbc(){
myClass.abc();
verify(myInterface).myMethodToBeVerified(new String(“a”), new String(“b”));
}
}
但是我得到了 mockito 想要但没有被调用,实际上在验证调用时与这个 mock 的交互为零。
谁能提出一些解决方案。
原文由 user3096719 发布,翻译遵循 CC BY-SA 4.0 许可协议
您需要在要测试的类中注入模拟。此刻你正在与真实对象交互,而不是与模拟对象交互。您可以通过以下方式修复代码:
尽管将所有初始化代码提取到
@Before
中是一个更明智的选择