Capturing Arguments with ArgumentCaptor
Use Mockito's ArgumentCaptor to capture and assert on the exact objects passed to a mock in complex scenarios.
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.
All lessons in this course
- Custom Answers and Callbacks
- Mocking Static Methods and Constructors
- Mockito Best Practices
- Capturing Arguments with ArgumentCaptor