Open In App
Related Articles

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 Class:


class Student {
    private Pen pen;
    public Student(Pen pen) {
        this.pen = pen;
    public String write() {
        return "Student Write with: " + pen.getRedPen();
} Class:


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 Class. Class:


class StudentTest {
    Pen pen;
    Student student;
    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



@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.

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 06 Dec, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials