Exception : mockito wanted but not invoked, Actually there were zero interactions with this mock
java, mockito, unit-testing
Solution
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"));
}
Problem
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.