Search code examples
javamockitoverify

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.


Solution

  • To simplify... Let's say you are testing method A with certain parameters. What method A does is calls methods B, C and D.

    With Mockito.verify you can test that methods B, C, D really are called. It even let's you specify more complex testing such as:

    • atLeast(1)
    • atMost(10)

    It can really be useful when the method you are testing behaves differently based on parameters you call it with.