Video in progress
What does verify() check in Mockito, and when do you use it?
verify checks whether a method on a mock was called, how many times, and with which arguments. It's used for actions that return no result but have an effect on the outside.
📖 Answer
If a method is void, or its job is "send a message to someone", you can't check the result with assertEquals. That's when you need verify:
service.cancel(orderId);
verify(paymentClient).refund(orderId); // exactly once
verify(notifier, times(2)).send(any()); // twice
verify(auditLog, never()).write(any()); // never called at all
verifyNoMoreInteractions(paymentClient); // no other calls happened
When you don't need it: if the method returns a result and that result shows everything, verify is redundant. Writing verify(repo).findById(...) for every stubbed findById ties the test to the internal structure of the code, and then an ordinary refactoring breaks dozens of tests.
A common rule: stub for queries, verify for commands.
Sign up to mark studied questions and save your progress.