Mockito @InjectMocks Annotation Tutorial

1. Overview

Mockito is a widely used testing framework in the Java ecosystem. It offers a range of annotations to simplify creating mock objects. One such annotation is @InjectMocks. This annotation automatically injects mock objects into the object being tested. In this tutorial, we'll explore the @InjectMocks annotation.

2. Development Steps

1. Set up a Maven project.

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

3. Design a service class with a dependency.

4. Compose a test class for the service.

5. Use @Mock to create mock objects and @InjectMocks to inject them into the service.

6. Define the test cases.

7. Run the tests and observe the results.

3. Dependencies (Mockito and JUnit 5)

Step 1 and Step 2: Create a simple Maven project and add the following Mockito and JUnit 5 dependencies:
<!-- 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-junit-jupiter</artifactId>
    <version>5.6.0</version>
    <scope>test</scope>
</dependency>

4. Code Program

// Step 3: Design a Dependency and a Service class
class DependencyService {
    String getMessage() {
        return "Real message from DependencyService";
    }
}
class MainService {
    private DependencyService dependencyService;
    MainService(DependencyService dependencyService) {
        this.dependencyService = dependencyService;
    }
    String fetchMessage() {
        return "Main Service: " + dependencyService.getMessage();
    }
}
// Step 4 and 5: Compose a test class for MainService
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class MainServiceTest {
    @Mock
    private DependencyService dependencyServiceMock;
    @InjectMocks
    private MainService mainService;
    @Test
    public void fetchMessageTest() {
        // Define behavior for the mock object
        when(dependencyServiceMock.getMessage()).thenReturn("Mocked message from DependencyService");
        String result = mainService.fetchMessage();
        assertEquals("Main Service: Mocked message from DependencyService", result);
    }
}

Output:

The test fetchMessageTest will pass successfully with the expected output.

Code Explanation:

1. Dependencies required for Mockito and JUnit 5 are first set up.

2. We created two classes. The first is DependencyService, which acts as a dependency for MainService.

3. The @Mock annotation creates a mock object for DependencyService in the test class.

4. The @InjectMocks annotation is then utilized to automatically inject the mock object into MainService.

5. In the test method fetchMessageTest, the behavior of the mock object is defined using the when...thenReturn construct.

6. We then invoke the method on MainService and assert the result, which should now use the mocked response.

5. Conclusion

The @InjectMocks annotation in Mockito provides an efficient way to automatically inject mock objects into the object being tested. It helps simplify the test setup and make the unit tests more concise. When used with other Mockito annotations, it can be a powerful tool in a developer's unit testing toolkit.

Related Mockito Annotations

Comments