Difference Between @Mock and @InjectMocks in Mockito

Improve Article

Save Article

Like Article

Mockito is an open-source testing framework used for unit testing of Java applications. It plays a vital role in developing testable applications. Mockito is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in Unit Testing. Unit Testing is a type of software testing in which individual software components are tested. The primary objective of using the Mockito framework is to simplify the development of a test by mocking external dependencies and using them in the test code. And as a result, Mockito provides a simpler test code that is easier to understand, more readable, and modifiable. Mockito can also be used with other testing frameworks like JUnit and TestNG. So in this article, we will discuss the differences between @Mock and @InjectMocks which are the two most important and confusing annotations that are available in the Mockito framework. 

Main Difference 

If we are talking about the main difference then in simple terms we can say

@Mock creates a mock, and @InjectMocks creates an instance of the class and injects the mocks that are created with the @Mock annotations into this instance.

Let’s understand the above statement with a simple example in Java. 

Understand the Difference with an Example

Suppose we have two classes named Student and Pen. And the code for both these classes is as follows 

Student.java Class:

Java

class Student {

  

    private Pen pen;

  

    public Student(Pen pen) {

        this.pen = pen;

    }

  

    public String write() {

        return "Student Write with: " + pen.getRedPen();

    }

  

}

Pen.java Class:

Java

class Pen {

  

    private String redPen;

  

    public Pen(String redPen) {

        this.redPen = redPen;

    }

  

    String getRedPen() {

        return redPen;

    }

}

From the above code, you can see the Student class need Pen to perform the write operation. Now let’s perform unit testing for the Student.java Class.

StudentTest.java Class:

Java

@RunWith(MockitoJUnitRunner.class)

class StudentTest {

  

    @Mock

    Pen pen;

  

    @InjectMocks

    Student student;

  

    @Test

    public void writeWithPenTest() throws Exception {

        Mockito.when(pen.getRedPen()).thenReturn("Red Pen");

        assertEquals("Student Write with: Red Pen", student.write());

    }

  

}

So what happens in the code is Mockito will mock a Pen class and its behavior using the when and thenReturn method. And at last, using the @InjectMocks Mockito will put that Pen into Student class. And one more thing you can notice is that you don’t even have to create a new Student object. Mockito will inject it for you.

// we don't have to do this
Student student = new Student(pen);

Difference Table

@InjectMocks

@Mock

@Mock creates a mock. @InjectMocks creates an instance of the class and injects the mocks that are created with the @Mock annotations into this instance.

@Mock is used to create mocks that are needed to support the testing of the class to be tested. @InjectMocks is used to create class instances that need to be tested in the test class.

Annotated class to be tested dependencies with @Mock annotation. @InjectMocks is used when the actual method body needs to be executed for the given class.

We must define the when-thenReturn methods for mock objects and which class methods will be invoked during actual test execution. Use @InjectMocks when we need all internal dependencies initialized with mock objects to work the method correctly.