Mockito eq() Method Example

1. Introduction

Mockito's eq() method is used to define specific argument values while setting up stubs or verifying interactions with mocks. This method ensures that the mock only matches the specified value, allowing testers to be exact about their expectations.

2. Example Steps

1. Define an interface representing a message service we wish 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 eq() method to match a specific argument value.

6. Test the stubbed behavior with the expected and other values.

7. Verify interactions on the mock.

3. Mockito eq() 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.eq;

// Step 1: Define an interface
interface MessageService {
    String deliverMessage(String recipient);
}

// Step 2: Implement the interface
class SMSMessageService implements MessageService {
    @Override
    public String deliverMessage(String recipient) {
        return "Message delivered to: " + recipient;
    }
}

public class MessageTest {

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

        // Step 5: Stub the deliverMessage method to return a fixed response for a specific recipient
        when(mockedService.deliverMessage(eq("John"))).thenReturn("Mocked response for John");

        // Step 6: Call the stubbed method with the expected and a different recipient
        String response1 = mockedService.deliverMessage("John");
        String response2 = mockedService.deliverMessage("Jane");

        // Step 7: Verify that the mock was called with "John" as its recipient
        verify(mockedService).deliverMessage(eq("John"));
    }
}

Output:

The test will pass successfully, response1 will contain the value "Mocked response for John", and response2 will be null as there was no stubbed behavior defined for the recipient "Jane".

4. Step By Step Explanation

1. We start by defining a MessageService interface simulating a message delivery service.

2. The SMSMessageService class implements this interface, returning a message indicating the delivery status.

3. In the MessageTest class, we establish our mock for the MessageService interface.

4. With Mockito's when() method combined with eq("John"), we dictate that when the deliverMessage method is called with "John" as its recipient, a static response "Mocked response for John" should be returned.

5. During testing, we invoke the stubbed method with "John" and another name, "Jane".

6. Due to our stubbing with eq(), only the invocation with "John" returns the mocked response, while the call with "Jane" returns null, as we haven't defined any stubbed behavior for that input.

7. Finally, using verify(), we confirm that our mock's deliverMessage method was called with "John" as its recipient.


Comments