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

后端 未结 4 1859
旧时难觅i
旧时难觅i 2020-12-08 06:20

I have interface

Interface MyInterface {
  myMethodToBeVerified (String, String);
}

And implementation of interface is

clas         


        
4条回答
  •  北海茫月
    2020-12-08 07:03

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

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

    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

    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"));
         }
    }
    

提交回复
热议问题