How to Use ArgumentCaptor in Mockito with JUnit Tests
Use
ArgumentCaptor in Mockito to capture arguments passed to mocked methods during JUnit tests. Declare an ArgumentCaptor for the argument type, invoke the method, then use capture() to get the argument and assert its value.Syntax
The basic syntax for using ArgumentCaptor involves creating an instance for the argument type you want to capture, then passing it to verify() on the mock to capture the argument. Finally, retrieve the captured value with getValue() or getAllValues().
ArgumentCaptor<Type> captor = ArgumentCaptor.forClass(Type.class);- create captorverify(mock).method(captor.capture());- capture argumentType captured = captor.getValue();- get captured argument
java
ArgumentCaptor<Type> captor = ArgumentCaptor.forClass(Type.class);
verify(mock).method(captor.capture());
Type captured = captor.getValue();Example
This example shows how to capture a String argument passed to a mocked service method and assert its value in a JUnit test using Mockito.
java
import static org.mockito.Mockito.*; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; class MyService { void sendMessage(String message) { // sends a message } } public class ArgumentCaptorTest { @Test void testArgumentCaptor() { MyService mockService = mock(MyService.class); // Call method with argument mockService.sendMessage("Hello World"); // Create ArgumentCaptor for String ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); // Verify method called and capture argument verify(mockService).sendMessage(captor.capture()); // Assert captured argument assertEquals("Hello World", captor.getValue()); } }
Output
Test passed
Common Pitfalls
- Not using
verify()withcaptor.capture()causes no argument to be captured. - Capturing arguments from methods not called will fail the test.
- Using
getValue()when multiple calls happen captures only the last argument; usegetAllValues()for all. - For primitive types, use wrapper classes (e.g.,
Integerinstead ofint).
java
/* Wrong way: missing capture() in verify */ // verify(mockService).sendMessage("Hello World"); // No capture, no argument stored /* Right way: capture argument */ // verify(mockService).sendMessage(captor.capture());
Quick Reference
| Step | Description | Code snippet |
|---|---|---|
| 1 | Create ArgumentCaptor for argument type | ArgumentCaptor |
| 2 | Call method on mock with argument | mock.method(arg); |
| 3 | Verify method call and capture argument | verify(mock).method(captor.capture()); |
| 4 | Get captured argument value | Type captured = captor.getValue(); |
| 5 | Assert captured value | assertEquals(expected, captured); |
Key Takeaways
Use ArgumentCaptor to capture and inspect arguments passed to mock methods in JUnit tests.
Always use verify(mock).method(captor.capture()) to capture arguments correctly.
Use getValue() for single calls and getAllValues() for multiple calls.
Remember to create ArgumentCaptor with the correct argument type.
Avoid missing capture() in verify to prevent tests from silently passing without capturing.