If you use the MockitoJUnitRunner
to run your test, then an ArgumentCaptor
will be created for you, and there's no need to initialise it explicitly. If you do that, then you can also use the @Mock
annotation to create your mock objects, that is, you don't need the mock
static method any more. Your test class would then begin like this.
@RunWith(MockitoJUnitRunner.class)
public class WidgetStatusServletTest {
@Captor private ArgumentCaptor<ArrayList<WidgetStatus>> captor;
@Mock private DBController mockController;
@Mock private HttpServletRequest mockRequest;
@Mock private HttpServletResponse mockResponse;
@Mock private Repository mockRepository;
This question addresses the issue of whether to use MockitoJUnitRunner
, or an explicit call to MockitoAnnotations.initMocks
.
Just a side note - your test methods will be much easier to read if you use variable names that clearly indicate that your objects are mocks, like I've done here. Otherwise, in a long test class with many different variables, it's easy to lose track of which variables reference mocks, and which reference "real" objects.