Verifying interactions
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
check that an important call happened, and only that.
Planned topics
verify(mock).method(arg)checks the call happened once.verify(mock, never())andtimes(n)check absence and counts.- Verify calls with real side effects (send email, charge card), not every internal call.
- Verifying everything freezes the implementation; refactoring then breaks tests that should pass.
- Prefer asserting on results and state when you can.
Draft outline details
Demonstration
service.placeOrder(cmd);
verify(emailSender).sendConfirmation(order.id());
verify(gateway, never()).refund(any());Check your understanding
Name one call worth verifying.
Show answer
One with a side effect outside the object, such as sending an email.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.