Q:

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

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.

A:

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
void setUp(){
     myClass = new myClass();
     myClass.myObj = myInteface;
}

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

A:

Your class MyClass creates a new MyClassToBeTested, instead of using your mock.  My article on the Mockito wiki describes two ways of dealing with this.

A:

@Jk1's answer is fine, but Mockito also allows for more succinct injection using annotations:
@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).

