Mockito ArgumentMatchers

1. Overview

In Mockito, while writing test cases, there may arise situations where you don't care about the input argument of a method call; you just want the method to return a certain value or behave in a specific way regardless of what is passed to it. This is where ArgumentMatchers comes into play. They allow you to specify generic arguments that match certain criteria instead of specific values. In this tutorial, we'll dive deep into the ArgumentMatchers class and see how it can be utilized.

2. Development Steps

1. Set up a new Maven project.

2. Add the necessary Mockito and JUnit 5 dependencies.

3. Create a simple class named 'BookService' to showcase the use of ArgumentMatchers.

4. Craft a test class to demonstrate the use of various ArgumentMatchers.

5. Execute the tests and interpret the results.

6. Understand the importance of ArgumentMatchers.

3. Dependencies (Mockito and JUnit 5)

<!-- JUnit 5 -->
<!-- Mockito -->

4. Code Program

// Step 3: Define the BookService class
class BookService {
    public boolean isValidBook(String title, int yearPublished) {
        return title != null && !title.trim().isEmpty() && yearPublished > 0;
// Step 4: Create the test class for BookService
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.*;
public class BookServiceTest {
    public void testArgumentMatchers() {
        // Mocking the BookService
        BookService mockService = mock(BookService.class);
        // Use ArgumentMatchers to set expectations
        when(mockService.isValidBook(anyString(), anyInt())).thenReturn(true);
        // Test with various inputs
        assert mockService.isValidBook("Mockito Guide", 2020);
        assert mockService.isValidBook("Some Title", -50);
        // Verify the interactions
        verify(mockService).isValidBook(anyString(), anyInt());


The tests will run successfully, indicating that our mocked method behaves as expected, regardless of the specific arguments passed.

Code Explanation:

1. The BookService class has a method isValidBook which checks if a book's title is valid and if it was published in a positive year.

2. In our test, we've mocked BookService.

3. Using Mockito's when() method combined with ArgumentMatchers, we set our mock to return true whenever isValidBook is called with any string and any integer.

4. We then call isValidBook with two different sets of arguments, and in both cases, the method returns true.

5. The verify() method checks that our mocked method was called with any string and any integer, which ensures our mock interaction was as expected.

5. Conclusion

Mockito's ArgumentMatchers are an invaluable tool for creating flexible and concise mock behaviors. They enable you to specify behaviors based on generic argument conditions rather than specific values, making your tests more resilient to changes in business logic or code refactoring. Integrating them into your testing routine can simplify your setup and increase the robustness of your unit tests.

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