Mockito verify() method can be used to test number of method invocations too. We can test exact number of times, at least once, at least, at most number of invocation times for a mocked method. We can use verifyNoMoreInteractions() after all the verify() method calls to make sure everything is verified.
Mockito allows us to partially mock an object. This means that we can create a mock object and still be able to call a real method on it. To call a real method on a mocked object we use Mockito's thenCallRealMethod().
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(...);
Annotate the non-mock object with @Spy
annotation and then check for verify()
. Check this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With