Capturing Arguments with ArgumentCaptor
Use Mockito's ArgumentCaptor to capture and assert on the exact objects passed to a mock in complex scenarios.
Capturing Arguments with ArgumentCaptor is a free Testing Mastery: JUnit, Mockito & Integration Tests lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Testing Mastery: JUnit, Mockito & Integration Tests learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Inspecting What Was Passed
Sometimes verifying that a method was called is not enough. You need to assert on the actual argument the code constructed and passed. Mockito's ArgumentCaptor grabs that value for inspection.
Matchers vs Captors
Argument matchers like eq(...) check inputs during verification. A captor instead records the argument so you can run rich assertions afterward.
Creating a Captor
Declare a captor for the type you expect to capture.
ArgumentCaptor<User> captor =
ArgumentCaptor.forClass(User.class);Capturing During Verify
Pass the captor's capture() where the argument would go inside verify.
verify(repo).save(captor.capture());Reading the Value
After verification, retrieve the captured object and assert on its fields.
User saved = captor.getValue();
assertEquals("Ada", saved.getName());Full Example
Here we confirm the service builds the correct user before saving it.
service.register("Ada", "ada@x.io");
verify(repo).save(captor.capture());
assertEquals("ada@x.io",
captor.getValue().getEmail());Capturing Multiple Calls
If the method was called several times, use getAllValues() to get every captured argument in order.
verify(repo, times(2)).save(captor.capture());
List<User> all = captor.getAllValues();The @Captor Annotation
You can declare captors as fields with @Captor when using the Mockito extension, reducing boilerplate.
@Captor
ArgumentCaptor<User> captor;Captor or Matcher?
Use a matcher for simple equality checks at call time. Reach for a captor when the argument is complex, built internally, or needs multiple assertions.
Avoid Overuse
Captors couple tests to internal construction details. Prefer them for genuinely complex arguments, not for everything, to keep tests resilient.
Why It Matters
Captors let you verify the precise data your code hands to collaborators, catching subtle mapping or transformation bugs.
Quick Check
How do you retrieve the captured argument after verification?
Recap
You learned argument capturing:
ArgumentCaptor.forClassor@Captorcreates a captor- Pass
capture()insideverify getValue()/getAllValues()read the captured data- Use captors for complex, internally built arguments
Frequently asked questions
Is the “Capturing Arguments with ArgumentCaptor” lesson free?
Yes — the full text of “Capturing Arguments with ArgumentCaptor” is free to read here on the web, and the Testing Mastery: JUnit, Mockito & Integration Tests course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Testing Mastery: JUnit, Mockito & Integration Tests course, upgrade to CoddyKit PRO.
What will I learn in “Capturing Arguments with ArgumentCaptor”?
Use Mockito's ArgumentCaptor to capture and assert on the exact objects passed to a mock in complex scenarios. You practise Testing Mastery: JUnit, Mockito & Integration Tests with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Testing Mastery: JUnit, Mockito & Integration Tests?
No prior experience is required. Testing Mastery: JUnit, Mockito & Integration Tests on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Capturing Arguments with ArgumentCaptor” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Testing Mastery: JUnit, Mockito & Integration Tests lesson?
Yes. Every Testing Mastery: JUnit, Mockito & Integration Tests lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Custom Answers and Callbacks
- Mocking Static Methods and Constructors
- Mockito Best Practices
- Capturing Arguments with ArgumentCaptor