Mockito any() Method Example

1. Introduction

Mockito's any() method is a powerful tool within the Mockito library that enables testers to set up stubbed behavior or verify interactions without being specific about the exact argument values. This becomes handy when the exact arguments are not relevant to the test or when they are hard to predict.

2. Example Steps

1. Define an interface to represent a service we want to mock.

2. Implement the interface with a concrete class.

3. Set up a test class for the service.

4. Mock the interface using Mockito.

5. Stub a method's behavior using Mockito's any() method to accept any argument of a specific type.

6. Test the stubbed behavior with different arguments.

7. Verify interactions on the mock.

3. Mockito any() Method Example

import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.any;

// Step 1: Define an interface
interface NotificationService {
    String sendNotification(String message);
}

// Step 2: Implement the interface
class EmailNotificationService implements NotificationService {
    @Override
    public String sendNotification(String message) {
        return "Notification sent: " + message;
    }
}

public class NotificationTest {

    @Test
    public void testAnyMethodWithMockito() {
        // Step 4: Mock the NotificationService interface
        NotificationService mockedService = mock(NotificationService.class);

        // Step 5: Stub the sendNotification method to return a fixed response for any string input
        when(mockedService.sendNotification(any(String.class))).thenReturn("Mocked response");

        // Step 6: Call the stubbed method with different arguments
        String response1 = mockedService.sendNotification("Hello");
        String response2 = mockedService.sendNotification("Goodbye");

        // Step 7: Verify that the mock was called with any string for both interactions
        verify(mockedService).sendNotification(any(String.class));
    }
}

Output:

The test will pass successfully, and both response1 and response2 will contain the value "Mocked response".

4. Step By Step Explanation

1. We begin by defining a NotificationService interface to simulate a notification-sending service.

2. The EmailNotificationService class implements this interface, returning a message indicating that the notification was sent.

3. Within the NotificationTest class, we set up our mock for the NotificationService interface.

4. Using Mockito's when() method in conjunction with any(String.class), we stipulate that for any string input to sendNotification, a static response "Mocked response" should be returned.

5. In the test, we call the stubbed method with two different strings, "Hello" and "Goodbye".

6. Even though the inputs are different, due to our stubbing with any(), both invocations return the same mocked response.

7. Finally, we use verify() to ensure that our mock's sendNotification method was called with any string as its argument.


Comments