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

后端 未结 2 1003
南方客
南方客 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: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(...);
    

提交回复
热议问题