Skip to content
Ameen Abozaid
Planned lessonJNR-044-L04

Argument captors

  • Lesson 4 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

inspect the object that was passed to a collaborator.

Planned topics

  • ArgumentCaptor<T> records the argument of a call.
  • Use it when the code builds an object and hands it away (for example a saved Order).
  • Capture with verify(...).save(captor.capture()), then captor.getValue().
  • Assert on the captured object with AssertJ.
  • Do not capture when you can assert on a returned value.
Draft outline details

Demonstration

java
var captor = ArgumentCaptor.forClass(Order.class);
verify(orderRepository).save(captor.capture());
assertThat(captor.getValue().status()).isEqualTo(OrderStatus.PAID);

Check your understanding

When is a captor useful?

Show answer

When the object you want to check is passed to a collaborator, not returned.

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.