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