Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito, mission of method verify

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.

like image 449
Maria Pomazkina-Karpikova Avatar asked Dec 05 '25 09:12

Maria Pomazkina-Karpikova


1 Answers

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()!

like image 93
GhostCat Avatar answered Dec 07 '25 23:12

GhostCat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!