• Articles
  • Tutorials
  • Interview Questions
  • Webinars

Mockito Annotations and Basic Mocking

Table of Contents

Mockito Annotations: @Mock, @InjectMocks, and @Spy

Mockito annotations provide a convenient way to create and manage mock objects in your tests. They help simplify the process of mocking dependencies and injecting them into the class that is under test. This section will explore three important Mockito annotations: @Mock, @InjectMocks, and @Spy.

@Mock

The ‘@Mock’ annotation creates a mock object for a given class or interface. It instructs Mockito to create a proxy object that mimics the behavior of the real object. Using @Mock, you can simulate the behavior of dependencies without invoking their actual methods.

Here’s an example of using @Mock:

@RunWith(MockitoJUnitRunner.class)
public class MyTest {
    @Mock
    private SomeDependency dependency;
    
    // Rest of the test code
}

In the above example, Mockito creates a mock object for the ‘SomeDependency’ class, allowing you to stub its methods and verify interactions.

@InjectMocks

The ‘@InjectMocks’ annotation automatically injects mock objects into the class that is under test. It simplifies the process of setting up the test environment by automatically wiring the mock objects into the tested class.

Here’s an example of using @InjectMocks:

@RunWith(MockitoJUnitRunner.class)
public class MyTest {
    @Mock
    private SomeDependency dependency;
    
    @InjectMocks
    private MyClass myClass;
    
    // Rest of the test code
}

Here, Mockito injects the mock object (dependency) into the ‘MyClass’ instance (myClass) during test setup to allow you to test the behavior of MyClass without worrying about manually setting up its dependencies.

@Spy

The ‘@Spy’ annotation is used to create a partial mock object. It allows you to spy on a real object and selectively mock its methods. With @Spy, you can retain the original behavior of the object while stubbing specific methods as needed.

Here’s an example of using @Spy:

@RunWith(MockitoJUnitRunner.class)
public class MyTest {
    @Spy
    private SomeClass someObject;
    
    // Rest of the test code
}

In the above example, Mockito creates a spy object for the ‘SomeClass’ instance ‘someObject.’ You can then stub or verify specific methods of ‘someObject’ while retaining the original behavior of the unmocked methods.

Get 100% Hike!

Master Most in Demand Skills Now !

Creating Mock Objects with Mockito

Mock objects are crucial in isolating the code under test and verifying its behavior. Mockito provides several ways to create mock objects based on your needs. This section will explore different methods to create mock objects using Mockito.

Using the ‘@Mock’ annotation

As mentioned earlier, the ‘@Mock’ annotation is a convenient way to create mock objects. Mockito automatically creates a mock object for the corresponding class or interface when a particular field is annotated with ‘@Mock.’

Here’s an example:

@RunWith(MockitoJUnitRunner.class)
public class MyTest {
    @Mock
    private SomeDependency dependency;
    
    // Rest of the test code
}

In the above example, Mockito creates a mock object for the ‘SomeDependency’ class, which can be used to stub methods and verify interactions in your test cases.

Using the ‘Mockito.mock()’ method

Mock objects can be created using the ‘Mockito.mock()’ method as well.  This method takes a class or interface as an argument and returns a mock object of that type.

Here’s an example:

public class MyTest {
    private SomeDependency dependency;
    
    @Before
    public void setup() {
        dependency = Mockito.mock(SomeDependency.class);
    }
    
    // Rest of the test code
}

Here, employing the ‘Mockito.mock()’ method, a mock object is created for the ‘SomeDependency’ class, which is then assigned to the dependency field.

Using the ‘Mockito.spy()’ method

The ‘Mockito.spy()’ method allows you to create a partial mock object by wrapping an existing object. It retains the original behavior of the object while allowing you to stub or verify specific methods.

Here’s an example:

public class MyTest {
    private SomeClass someObject;
    
    @Before
    public void setup() {
        someObject = Mockito.spy(new SomeClass());
    }
    
    // Rest of the test code
}

Here, the ‘Mockito.spy()’ method is employed to not only wrap an instance of SomeClass but also create a spy object, ‘someObject,’ which can be used in your test cases.

Read on: Mockito Methods to Enhance your Knowledge!

Stubbing Methods and Returning Expected Values

Stubbing methods are a fundamental aspect of Mockito. It allows you to define the behavior of mock objects and specify the values they should return when their methods are called. Mockito provides different ways to stub methods and return expected values. Let’s explore some of the techniques.

Using ‘thenReturn()’

The ‘thenReturn()’ method is used to stub a method and specify the value it should return when invoked. You can chain multiple thenReturn() calls to specify different return values based on different scenarios.

Here’s an example:

@Test
public void testSomeMethod() {
    SomeDependency dependency = Mockito.mock(SomeDependency.class);
    Mockito.when(dependency.someMethod()).thenReturn("Hello, Mockito!");
    
    // Rest of the test code
}

Here, employing the ‘thenReturn()’ method, ‘someMethod()’ of the ‘SomeDependency’ mock object is stubbed, thereby instructing it to return the string “Hello, Mockito!” when the method is called.

Using ‘thenAnswer()’

The ‘thenAnswer()’ method allows you to provide a custom implementation to determine the return value based on the method arguments or other factors. It takes an instance of the Answer functional interface as an argument, where you can define the desired behavior.

Here’s an example:

@Test
public void testSomeMethod() {
    SomeDependency dependency = Mockito.mock(SomeDependency.class);
    Mockito.when(dependency.someMethod()).thenAnswer(invocation -> {
        // Custom logic to calculate the return value based on invocation details
        return "Dynamic value";
    });
    
    // Rest of the test code
}

Here, employing the ‘thenAnswer()’ method, the ‘someMethod()’ of the ‘SomeDependency’ mock object is stubbed with a custom implementation that dynamically calculates the return value.

Check out the Difference between Mockito Mock and SPY to Enhance Your Knowledge!

Verifying Method Invocations and Interactions

One of the key features of Mockito is the ability to verify whether specific methods were invoked on mock objects and understand how they interacted with them. This allows you to ensure that the expected interactions between objects are taking place during the test.

Let’s explore how to verify method invocations and interactions using Mockito.

  • Using ‘verify()’

The ‘verify()’ method determines whether a specific method was called on a mock object. You can specify the precise method, the number of invocations, and the sequence in which the invocations must be verified.

Here’s an example:

@Test
public void testSomeMethod() {
    SomeDependency dependency = Mockito.mock(SomeDependency.class);
    
    // Invoke the method on the mock object
    
    Mockito.verify(dependency).someMethod();
}

In the example from above, availing the ‘verify()’ method, we can verify whether the ‘someMethod()’ of the ‘SomeDependency’ mock object was called at least once during the test.

  • Using ‘verify()’ with ‘times()’

A combination of the ‘verify()’ and ‘times()’ methods can be employed to specify the exact number of invocations to be verified. This can prove useful when ensuring that a method has been called a specific number of times.

Here’s an example:

@Test
public void testSomeMethod() {
    SomeDependency dependency = Mockito.mock(SomeDependency.class);
    
    // Invoke the method on the mock object multiple times
    
    Mockito.verify(dependency, Mockito.times(3)).someMethod();
}

In the above example, the ‘verify()’ method is used with ‘times(3)’ to verify that the ‘someMethod()’ of the ‘SomeDependency’ mock object was called exactly three times during the test.

  • Using ‘verify()’ with argument matching

The ‘verify()’ method can also be used with argument matching to verify that a specific method was called with specific arguments.

Here’s an example:

@Test
public void testSomeMethod() {
    SomeDependency dependency = Mockito.mock(SomeDependency.class);
    
    // Invoke the method on the mock object with specific arguments
    
    Mockito.verify(dependency).someMethod(Mockito.eq("argument"));
}

The ‘verify()’ method is employed along with ‘eq(“argument”)’ in the above example in order to verify whether the ‘someMethod()’ of the ‘SomeDependency’ mock object was called with the argument “argument” during the test.
Certification in Bigdata Analytics
Using these verification techniques ensures that the desired method invocations and interactions occur as expected in your tests.

Course Schedule

Name Date Details
Python Course 13 Jul 2024(Sat-Sun) Weekend Batch
View Details
Python Course 20 Jul 2024(Sat-Sun) Weekend Batch
View Details
Python Course 27 Jul 2024(Sat-Sun) Weekend Batch
View Details

About the Author

Senior Consultant Analytics & Data Science

Presenting Sahil Mattoo, a Senior Consultant Analytics & Data Science at Eli Lilly and Company is an accomplished professional with 14 years of experience across data science, analytics, and technical leadership domains, demonstrates a remarkable ability to drive business insights. Sahil holds a Post Graduate Program in Business Analytics and Business Intelligence from Great Lakes Institute of Management.