Table of contents
1.
Introduction
2.
Mockito Annotations
3.
@Mock Annotation
4.
@RunWith Annotation
5.
@InjectMocks Annotation
6.
@Captor Annotation
7.
@Spy Annotation
8.
FAQs
9.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Mockito Annotations

Author Parth Jain
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Mockito is a framework that provides diverse numbers of annotations to help make coding easy to implement and fun to understand. With annotations, we can significantly reduce the lines of code that increase a programmer's confidence and focus on the business logic. We can also use annotations to avoid calling methods multiple times at different places. 
This blog will help you understand all the Mockito Annotations with examples.

Mockito Annotations

Mockito has the following annotations.

  • @Mock Annotation
  • @RunWith Annotation
  • @InjectMocks Annotation
  • @Captor Annotation
  • @Spy Annotation

@Mock Annotation

The @Mock Annotation is used to mock the objects that assist in minimizing the repeated mock objects. @Mock makes the test code and verification error convenient to read as the parameter names are used to identify the mocks.@Mock Annotation is available in the org.mockito package.
Consider the code below that does not use @mock 

@Test
public void withoutMock() {
    List dataList = Mockito.mock(ArrayList.class);
    
    dataList.add("six");
    Mockito.verify(dataList).add("six");
    assertEquals(0, dataList.size());


    Mockito.when(dataList.size()).thenReturn(100);
    assertEquals(100, dataList.size());
}

Now consider the same code with the use of @mock.

@Mock
List<String> dataList;

@Test
public void withMock() {
    dataList.add("six");
    Mockito.verify(dataList).add("six");
    assertEquals(0, dataList.size());

    Mockito.when(dataList.size()).thenReturn(100);
    assertEquals(100, dataList.size());
}

@RunWith Annotation

@RunWith is a class-level annotation and is used to keep the test clean. It helps improve debugging while detecting the unused stubs available in the test. It is available in the org.mockito.junit package.
Consider a sample snippet below.

@RunWith(MockitoJUnitRunner.class)  
public class ToDoWorkMock {  
.....  
}  

@InjectMocks Annotation

The @InjectMocks help mark a field or parameter upon which injection should be performed.@InjectMocks also enables mocks and spy injections while minimizing the repetitive mocks or spy injection in Mock. It is available in the org.mockito package.
Consider the code below.

@Mock
Map<String, String> testMap;

@InjectMocks
TestDictionary dic = new TestDictionary();

@Test
public void usingInjectMocks() {
    Mockito.when(testMap.get("Word")).thenReturn("Meaning");

    assertEquals("Meaning", dic.getMeaning("Word"));
}
Here is the class TestDictionary:
public class TestDictionary {
    Map<String, String> testMap;

    public TestDictionary() {
        testMap = new HashMap<String, String>();
    }
    public void add(final String word, final String meaning) {
        testMap.put(word, meaning);
    }
    public String getMeaning(final String word) {
        return testMap.get(word);
    }
}

In the above code, we used @InjectMocks to inject the mock testMap into the TestDictionary.

@Captor Annotation

@Captor enables the creation of a field-level argument captor.@Captor is also used with the Mockito's verify() method to receive the values passed once a method is called. Similar to the other annotations, the @Captor Annotation is available in the org.mockito package.
Consider the code below where we use an ArgumentCaptor.

@Test
public void withoutCaptor() {
    List dataList = Mockito.mock(List.class);
    ArgumentCaptor<String> arg = ArgumentCaptor.forClass(String.class);

    dataList.add("six");
    Mockito.verify(dataList).add(arg.capture());
    assertEquals("six", arg.getValue());
}
Now we will use @Captor for the same purpose.
@Mock
List dataList;

@Captor 
ArgumentCaptor argCaptor;

@Test
public void withCaptor() {
    dataList.add("six");
    Mockito.verify(dataList).add(argCaptor.capture());

    assertEquals("six", argCaptor.getValue());
}

@Spy Annotation

@Spy enables the creation of a partially mock object. Alternatively, @Spy allows the shorthand wrapping of the field instances in a spy object. Similar to other annotations, @Spy Annotation is available in the org.mockito package.
Consider the code below.

@Test
public void withoutSpy() {
    List<String> dataList = Mockito.spy(new ArrayList<String>());    
    dataList.add("six");
    dataList.add("five");

    Mockito.verify(dataList).add("six");
    Mockito.verify(dataList).add("five");

    assertEquals(2, dataList.size());

    Mockito.doReturn(100).when(dataList).size();
    assertEquals(100, dataList.size());
}
Now we will use @spy annotation to implement the same code. 
@Spy
List<String> dataList = new ArrayList<String>();

@Test
public void withSpy() {
    dataList.add("six");
    dataList.add("five");

    Mockito.verify(dataList).add("six");
    Mockito.verify(dataList).add("five");

    assertEquals(2, dataList.size());

    Mockito.doReturn(100).when(dataList).size();

FAQs

  1. What is @Mock Annotation?
    The @Mock Annotation is used to mock the objects that assist in minimizing the repeated mock objects. 
  2. What is @RunWith Annotation?
    @RunWith is a class-level annotation and is used to keep the test clean. It helps improve debugging while detecting the unused stubs available in the test. 
  3. What is @InjectMocks Annotation?
    The @InjectMocks helps mark a field or parameter upon which injection should be performed.@InjectMocks also enables mocks and spy injections while minimizing the repetitive mocks or spy injection.
  4. What is @Captor Annotation?
    @Captor enables the creation of a field-level argument captor.@Captor is also used with the Mockito's verify() method to receive the values passed once a method is called. 
  5. What is @Spy Annotation?
    @Spy enables the creation of a partially mock object. Alternatively, @Spy allows the shorthand wrapping of the field instances in a spy object. 

Key Takeaways

In this article, we have extensively discussed the various Mockito Annotations with practical examples. If you are Preparing for interview and don't know where to start, we have got you covered, check out our expert curated courses on our website, You can also check out Coding Ninjas Studio to practice frequently asked interview problems. We hope that this blog has helped you enhance your knowledge regarding Mockito Annotations and if you would like to learn more, check out our articles. Do upvote our blog to help other ninjas grow. Happy Coding!”

Live masterclass