This document will cover the following annotations of the Mockito library: @Mock, @Spy, @Captor and @InectMocks
There are different ways to enable the use of annotations with Mockito tests.
The first option we have is to annotate the JUnit test with a MockitoJUnitRunner
@ExtendWith(MockitoExtension.class)
public class MockitoAnnotationUnitTest {
...
}
Alternatively, we can enable Mockito annotations programmatically by invoking MockitoAnnotations.openMocks():
@BeforeEach
public void init() {
MockitoAnnotations.openMocks(this);
}
Lastly, we can use a MockitoJUnit.rule():
public class MockitoAnnotationsInitWithMockitoJUnitRuleUnitTest {
@Rule
public MockitoRule initRule = MockitoJUnit.rule();
...
}
In this case, we must remember to make our rule public.
The most widely used annotation in Mockito is @Mock. We can use @Mock to create and inject mocked instances
without having to call Mockito.mock manually.
In the following example, we’ll create a mocked ArrayList manually without using the @Mock annotation:
@Test
public void whenNotUseMockAnnotation_thenCorrect() {
List mockList = Mockito.mock(ArrayList.class);
mockList.add("one");
Mockito.verify(mockList).add("one");
assertEquals(0, mockList.size());
Mockito.when(mockList.size()).thenReturn(100);
assertEquals(100, mockList.size());
}