Java 例外:mockito 想要但未调用,实际上与此模拟的交互为零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20551926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:44:56  来源:igfitidea点击:

Exception : mockito wanted but not invoked, Actually there were zero interactions with this mock

javaunit-testingmockito

提问by user3096719

I have interface

我有接口

Interface MyInterface {
  myMethodToBeVerified (String, String);
}

And implementation of interface is

接口的实现是

class MyClassToBeTested implements MyInterface {
   myMethodToBeVerified(String, String) {
    …….
   }
}

I have another class

我还有一堂课

class MyClass {
    MyInterface myObj = new MyClassToBeTested();
    public void abc(){
         myObj.myMethodToBeVerified (new String(“a”), new String(“b”));
    }

}

}

I am trying to write JUnit for MyClass. I have done

我正在尝试为 MyClass 编写 JUnit。我已经做好了

class MyClassTest {
    MyClass myClass = new MyClass();

    @Mock
    MyInterface myInterface;

    testAbc(){
         myClass.abc();
         verify(myInterface).myMethodToBeVerified(new String(“a”), new String(“b”));
    }
}

But I am getting mockito wanted but not invoked, Actually there were zero interactions with this mockat verify call.

但是我得到了 想要的模拟但没有被调用,实际上在验证调用时与这个模拟的交互为零

can anyone suggest some solutions.

任何人都可以提出一些解决方案。

回答by Jk1

You need to inject mock inside the class you're testing. At the moment you're interacting with the real object, not with the mock one. You can fix the code in a following way:

您需要在您正在测试的类中注入模拟。目前您正在与真实对象交互,而不是与模拟对象交互。您可以通过以下方式修复代码:

void testAbc(){
     myClass.myObj = myInteface;
     myClass.abc();
     verify(myInterface).myMethodToBeVerified(new String("a"), new String("b"));
}

although it would be a wiser choice to extract all initialization code into @Before

尽管将所有初始化代码提取到 @Before

@Before
void setUp(){
     myClass = new myClass();
     myClass.myObj = myInteface;
}

@Test
void testAbc(){
     myClass.abc();
     verify(myInterface).myMethodToBeVerified(new String("a"), new String("b"));
}

回答by Kevin Welker

@Jk1's answer is fine, but Mockito also allows for more succinct injection using annotations:

@Jk1 的回答很好,但 Mockito 还允许使用注释进行更简洁的注入:

@InjectMocks MyClass myClass; //@InjectMocks automatically instantiates too
@Mock MyInterface myInterface

But regardless of which method you use, the annotations are not being processed (not even your @Mock) unless you somehow call the static MockitoAnnotation.initMocks()or annotate the class with @RunWith(MockitoJUnitRunner.class).

但是无论您使用哪种方法,都不会处理注释(甚至不是您的@Mock),除非您以某种方式调用静态MockitoAnnotation.initMocks()或使用@RunWith(MockitoJUnitRunner.class).

回答by Dawood ibn Kareem

Your class MyClasscreates a new MyClassToBeTested, instead of using your mock. My article on the Mockito wikidescribes two ways of dealing with this.

您的班级MyClass创建了一个 new MyClassToBeTested,而不是使用您的模拟。 我在 Mockito wiki 上的文章描述了两种处理方法。

回答by vijaya kumar

@jk1 answer is perfect, since @igor Ganapolsky asked, why can't we use Mockito.mock here? i post this answer.

@jk1 的回答很完美,既然@igor Ganapolsky 问,为什么我们不能在这里使用 Mockito.mock?我发布这个答案。

For that we have provide one setter method for myobj and set the myobj value with mocked object.

为此,我们为 myobj 提供了一个 setter 方法,并使用模拟对象设置 myobj 值。

class MyClass {
    MyInterface myObj;

    public void abc() {
        myObj.myMethodToBeVerified (new String("a"), new String("b"));
    }

    public void setMyObj(MyInterface obj)
    {
        this.myObj=obj;
    }
}

In our Test class, we have to write below code

在我们的 Test 类中,我们必须编写以下代码

class MyClassTest {

MyClass myClass = new MyClass();

    @Mock
    MyInterface myInterface;

    @test
    testAbc() {
        myclass.setMyObj(myInterface); //it is good to have in @before method
        myClass.abc();
        verify(myInterface).myMethodToBeVerified(new String("a"), new String("b"));
     }
}