how to verify a method of a non-mock object is called?

后端 未结 2 1002
南方客
南方客 2020-12-25 09:35

It seems mockito only verifies whether a method of a mock object is called and the mock object always have something like doReturn().when(mock object)

B

相关标签:
2条回答
  • 2020-12-25 09:42

    Annotate the non-mock object with @Spy annotation and then check for verify(). Check this

    0 讨论(0)
  • 2020-12-25 09:52

    You can use a Mockito Spy for this. If you setup anotherObj as a spy you can verify method calls on that object. In your example you need to make sure that the call to foo uses the spy instead of an ordinary implementation of anotherObj. The spy is setup something like this:

    AnotherClass anotherObjSpy = Mockito.spy(new AnotherClass());
    // do stuff -- e.g. anotherObjSpy.foo(...);
    verify(anotherObjSpy).codePath1(...);
    
    0 讨论(0)
提交回复
热议问题