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

Viewed 224136

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

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 mock at verify call.

can anyone suggest some solutions.

5 Answers

This exception can also be thrown if say, you expect thisMethod() to execute, but it didn't. It didn't because it's inside a condition that was not met.

For example, if you have some unit test that says verify thisMethod() is executed, but in fact, it was not because varX and varY are not equal.

//method expected to be called.    
if( varX == varY){
     thisMethod();
  }

 //test
 Mockito.verify(foo).thisMethod();
Related