David David - 3 years ago 108
Java Question

Mockito verify the return of a spied object method

I know you can verify the times a spied object's method was called. Can you verify the result of the method call?

Something like the following?

verify(spiedObject, didReturn(true)).doSomething();

Answer Source

To verify the number of times it was invoked, use verify(spiedObject, times(x)).doSomething().

You should NOT be verifying the value returned from the spied object. It is not the object under test so why verify what it returns. Instead verify the behavior of the object under test in response to the value returned from the spy.

Also, if you don't KNOW what value will be returned by the spied object it would be better to use a mock instead of a spy.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download