The JUnit rule can be used instead of
MockitoJUnitRunner.
It requires JUnit at least 4.7.
This rule adds following behavior:
-
Initializes mocks annotated with
Mock,
so that explicit usage of MockitoAnnotations.initMocks(Object) is not necessary.
Mocks are initialized before each test method.
-
Validates framework usage after each test method. See javadoc for
Mockito.validateMockitoUsage().
-
Since 2.*, on test failure, stubbing warnings are printed to System output. They might be useful for debugging.
Example use:
public class ExampleTest {
@Rule
public MockitoRule rule = MockitoJUnit.rule();
@Mock
private List list;
@Test
public void shouldDoSomething() {
list.add(100);
}
}