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 the process of 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 -->
<!-- Mockito -->

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;
public class MainServiceTest {
    private DependencyService dependencyServiceMock;
    private MainService mainService;
    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);


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 - DependencyService, which acts as a dependency for MainService.

3. In the test class, the @Mock annotation is used to create a mock object for DependencyService.

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 in simplifying the test setup and making the unit tests more concise. When used in conjunction with other Mockito annotations, it can be a powerful tool in a developer's unit testing toolkit.

Related Mockito Annotations