Mockito Annotations
The Mockito framework provides a variety of annotations to make the code simple and easy to understand. Also, it reduces the lines of code that helps in focusing on the business logic. In Mockito, annotations are useful when we want to use the mocked object at different places to avoid calling the same methods multiple times.
The Mockito annotations are given below:
- @Mock: It is used to mock the objects that helps in minimizing the repetitive mock objects. It makes the test code and verification error easier to read as parameter names (field names) are used to identify the mocks. The @Mock annotation is available in the org.mockito package.
Following code snippet shows how to use the @mock annotation:
Note: The @Mock annotation is always used with the @RunWith annotation.
- @RunWith: It is a class-level annotation. It is used to keep the test clean and improves debugging. It also detects the unused stubs available in the test and initialize mocks annotated with @Mock annotation. The @RunWith annotation is available in the org.mockito.junit package.
Following code snippet shows how to use the @RunWith annotation:
In the above code snippet, the MockitoJUnitRunner class is used to check that all the mocks are created and autowired when needed.
- @InjectMocks: It marks a field or parameter on which the injection should be performed. It allows shorthand mock and spy injections and minimizes the repetitive mocks and spy injection. In Mockito, the mocks are injected either by setter injection, constructor injection, and property injection. The @InjectMocks annotation is available in the org.mockito package.
Following code snippet shows how to use the @InjectMocks annotation:
- @Captor: It allows the creation of a field-level argument captor. It is used with the Mockito’s verify() method to get the values passed when a method is called. Like other annotations, @Captor annotation is also available in the org.mockito package.
Following code snippet shows how to use the @Captor annotation:
- @Spy – It allows the creation of partially mock objects. In other words, it allows shorthand wrapping of the field instances in a spy object. Like other annotations, @Spy annotation is also available in the org.mockito package.
Following code snippet shows how to use the @Spy annotation:
Example of Mockito annotations (@Mock, @RunWith, @InjectMocks, @Captor, @Spy)
Here, we are going to create an example of testing by using the annotations @Mock, @RunWith, @InjectMocks, @Captor, @Spy. Following are the steps to create an example of Mockito annotations:
Step 1: Create an interface named ToDoService that contains two unimplemented methods.
ToDoService.java
Step 2: Create an implementation class named ToDoBusiness.
ToDoBusiness.java
Step 3: Create a mock test named ToDoBusinessMock for testing purposes. In the following code, we will use all the annotations that we have discussed above.
ToDoBusinessMock.java
Output
The following output shows that the test is successfully running using Mockito annotations.