Mockito mock()

1. Overview

Mockito is a popular mocking framework for Java and it offers both annotations and explicit methods to facilitate the creation and manipulation of mocks. One of the fundamental methods provided by Mockito is the mock() method. 

While annotations like @Mock are popular and efficient for mock creation, using the mock() method can often provide more flexibility in certain scenarios. In this tutorial, we'll dive into the mock() method, understanding its purpose and usage.

2. Development Steps

1. Set up a Maven project.

2. Add the necessary dependencies (JUnit 5 and Mockito).

3. Design a service class with a method.

4. Compose a test class for the service.

5. Utilize the mock() method to create a mock object.

6. Implement and execute the test cases.

7. Observe and analyze 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: Design the Service class
class MessageService {
    String getMessage() {
        return "Real message from the service";
    }
}
// Step 4 and 5: Frame a test class for MessageService
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MessageServiceTest {
    @Test
    public void getMessageTest() {
        // Create a mock of MessageService using the mock() method
        MessageService mockedService = mock(MessageService.class);
        // Define mock behavior
        when(mockedService.getMessage()).thenReturn("Mocked message from the service");
        // Execute the mocked method
        String result = mockedService.getMessage();
        // Assert the result
        assertEquals("Mocked message from the service", result);
    }
}

Output:

The getMessageTest will pass successfully, showcasing that the mock() method was used correctly to mock the behavior of the MessageService.

Code Explanation:

1. After integrating the Mockito and JUnit 5 dependencies, we set up a basic MessageService class that has a getMessage method to return a message.

2. In the test class, instead of using annotations like @Mock, we employed the mock() method from Mockito to explicitly create a mock of MessageService.

3. With the mock object in place, we defined its behavior using the when...thenReturn construct. We specified that whenever the getMessage method is invoked on our mock object, it should return the "Mocked message from the service".

4. Following this, we invoked the getMessage method on our mocked service and asserted the returned value to ensure our mock worked as expected.

5. Conclusion

The mock() method in Mockito is a powerful way to create mock objects explicitly. It's especially useful in situations where annotations might not be the best fit or when dealing with dynamic mock creation. With the mock() method in our testing toolkit, we can create more flexible and comprehensive 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

Comments