Argument captors
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()), thencaptor.getValue(). - Assert on the captured object with AssertJ.
- Do not capture when you can assert on a returned value.
Draft outline details
Demonstration
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.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.