Could you please explain a mission of mockito method verify? Documentation says, that this method checks, whether the method was called. But can you give an example when it is really useful? Usually in test method we invoke a method and then... check, that we have invoked it right now? Sounds weird.
It goes like this:
public class UnderTest {
private Foo foo;
public UnderTest(Foo foo) { this.foo = foo };
public void bar() { foo.foo(); }
}
Now assume that you provide a mocked foo instance to UnderTest. And you want to be sure that foo.foo() is invoked when bar() is called.
Then you use verify() to ensure that the expected call took place.
In other words: tests need to verify method behavior. Ideally they do that by asserting on values returned by the method under test. But not all methods return something. Then you might verify your code by at least checking that certain expected calls on objects owned/passed to the class under test did occur. And then you need verify()!
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