how to verify a method of a non-mock object is called?
Solution 1:
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(...);
Solution 2:
Annotate the non-mock object with @Spy
annotation and then check for verify()
. Check this