Mockito any()

1. Overview

The any() method in Mockito is an argument matcher that matches any instance of a given class, including null. It is particularly useful when one isn't concerned about the exact argument passed to a mock method but wants to specify a behavior or verify an invocation regardless of the passed argument. This tutorial walks you through the use of the any() method and illustrates its utility in different scenarios.

2. Development Steps

1. Initialize a new Maven project.

2. Add the necessary dependencies: JUnit 5 and Mockito.

3. Create a class that will be tested using Mockito.

4. Implement a test class to demonstrate the use of the any() method.

5. Run the test cases.

6. Review the test results.

3. Dependencies (Mockito and JUnit 5)

<!-- JUnit 5 -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.10.0</version>
    <scope>test</scope>
</dependency>
<!-- Mockito -->
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>5.6.0</version>
    <scope>test</scope>
</dependency>

4. Code Program

// Step 3: Create a UserService class that interacts with a User database
class UserService {
    boolean addUser(String username) {
        // Usually interacts with a database to add a user
        return true;
    }
}
// Step 4: Implement the test class for UserService
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.any;
public class UserServiceTest {
    @Test
    public void addUserTest() {
        // Create a mock of UserService
        UserService mockService = mock(UserService.class);
        // Define the mock's behavior: return true when addUser is called with any string
        when(mockService.addUser(any(String.class))).thenReturn(true);
        // Call the addUser method on the mock
        mockService.addUser("JohnDoe");
        // Verify that the addUser method was called with any string argument
        verify(mockService).addUser(any(String.class));
    }
}

Output:

The addUserTest will pass successfully, indicating that the mock's addUser method was invoked with any string argument as expected.

Code Explanation:

1. After adding Mockito and JUnit 5 to our project, we created a simple UserService class. This class has an addUser method that, in a real-world scenario, might interact with a database.

2. We then used Mockito's mock() method to produce a mock of the UserService.

3. Using the when() method combined with any(String.class), we defined the behavior of the mock: whenever the addUser method is called with any string argument, it should return true.

4. We then called the addUser method on the mock with the string "JohnDoe".

5. The use of verify() paired with any(String.class) confirms that the mock's addUser method was invoked with any string argument.

5. Conclusion

Mockito's any() method is an incredibly versatile tool in the Mockito toolkit, especially when the specific argument used in a method call is inconsequential. By employing any(), developers can easily stub and verify methods without getting bogged down by the details of argument specifics. It ensures that our tests focus on behavior rather than unnecessary implementation details.

Related Mockito Methods

Mockito mock()
Mockito spy()
Mockito when()
Mockito thenThrow()
Mockito verify()
Mockito times()
Mockito never()
Mockito any()
Mockito eq()
Mockito inOrder()
Mockito doReturn()
Mockito doThrow()
Mockito doAnswer()
Mockito timeout()
Mockito ArgumentMatchers

Comments