java - Initialising mock objects - Mockito - Stack Overflow
stackoverflow.com › questions › 15494926The first solution (with the MockitoAnnotations.initMocks) could be used when you have already configured a specific runner ( SpringJUnit4ClassRunner for example) on your test case. The second solution (with the MockitoJUnitRunner) is the more classic and my favorite. The code is simpler. Using a runner provides the great advantage of automatic validation of framework usage (described by @David Wallace in this answer ).
Getting Started with Mockito @Mock, @Spy, @Captor and ...
www.baeldung.com › mockito-annotationsIn 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()); }