0
0
JUnittesting~15 mins

Argument matchers (any, eq) in JUnit - Build an Automation Script

Choose your learning style9 modes available
Verify service method called with specific and any arguments using argument matchers
Preconditions (2)
Step 1: Call the service method with arguments: "testString" and 10
Step 2: Verify that the service method was called with the exact String "testString" and any Integer argument
Step 3: Verify that the service method was called with any String argument and the exact Integer 10
✅ Expected Result: The verification passes confirming the method was called with the expected argument matchers
Automation Requirements - JUnit 5 with Mockito
Assertions Needed:
Verify method call with eq("testString") and anyInt()
Verify method call with anyString() and eq(10)
Best Practices:
Use Mockito's argument matchers consistently
Avoid mixing raw values and matchers in the same method call
Use @ExtendWith(MockitoExtension.class) for Mockito initialization
Use descriptive test method names
Automated Solution
JUnit
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class ServiceTest {

    interface Service {
        void performAction(String text, Integer number);
    }

    @Test
    void testArgumentMatchers() {
        Service serviceMock = mock(Service.class);

        // Call the method with specific arguments
        serviceMock.performAction("testString", 10);

        // Verify method called with exact String and any Integer
        verify(serviceMock).performAction(eq("testString"), anyInt());

        // Verify method called with any String and exact Integer
        verify(serviceMock).performAction(anyString(), eq(10));
    }
}

This test uses Mockito to create a mock of the Service interface.

We call the performAction method with the arguments "testString" and 10.

Then, we verify two things:

  • The method was called with the exact string "testString" and any integer (using eq and anyInt).
  • The method was called with any string and the exact integer 10 (using anyString and eq).

This shows how to use argument matchers to flexibly check method calls without requiring exact matches on all parameters.

Using @ExtendWith(MockitoExtension.class) ensures Mockito initializes correctly with JUnit 5.

Common Mistakes - 3 Pitfalls
{'mistake': 'Mixing raw values and argument matchers in the same method call verification', 'why_bad': 'Mockito requires all arguments to be either matchers or raw values, not a mix. Mixing causes runtime errors.', 'correct_approach': 'Use argument matchers for all parameters or none. For example, use eq("value") instead of raw "value" when using matchers.'}
{'mistake': 'Not importing static methods for argument matchers', 'why_bad': 'Without static imports, the code becomes verbose and less readable, and sometimes compilation errors occur.', 'correct_approach': "Use static imports like 'import static org.mockito.ArgumentMatchers.anyInt;' for cleaner code."}
Not initializing Mockito properly with JUnit 5
Bonus Challenge

Now add tests verifying the method is called with any String and any Integer using argument matchers

Show Hint