Andrez Segovia

Validate the arguments passed to a method

The Mockito framework has the ArgumentCaptor class that can be use to validate the arguments passed to a mocked method.

import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

public class MyServiceTest {

    @Test
    public void testMethodArgument() {
        // Arrange
        MyDependency mockDependency = mock(MyDependency.class);
        MyService myService = new MyService(mockDependency);

        // Act
        myService.execute("testArgument");

        // Capture the argument
        ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
        verify(mockDependency).methodToBeCalled(argumentCaptor.capture());

        // Validate the captured argument
        String capturedArgument = argumentCaptor.getValue();
        assertEquals("testArgument", capturedArgument);
    }
}