0
0
JunitHow-ToBeginner ยท 3 min read

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 captor
  • verify(mock).method(captor.capture()); - capture argument
  • Type 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() with captor.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; use getAllValues() for all.
  • For primitive types, use wrapper classes (e.g., Integer instead of int).
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

StepDescriptionCode snippet
1Create ArgumentCaptor for argument typeArgumentCaptor captor = ArgumentCaptor.forClass(Type.class);
2Call method on mock with argumentmock.method(arg);
3Verify method call and capture argumentverify(mock).method(captor.capture());
4Get captured argument valueType captured = captor.getValue();
5Assert captured valueassertEquals(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.