Skip to content
Ameen Abozaid
Planned lessonJNR-044-L03

Verifying interactions

  • Lesson 3 of 6
  • Junior
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()) and times(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

java
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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.