public class MockitoJUnitRunner extends Runner implements Filterable
MockitoJUnitRunner.StrictStubs
which automatically
detects stubbing argument mismatches and is planned to be the default in Mockito v3.
Runner is compatible with JUnit 4.4 and higher and adds following behavior:
UnnecessaryStubbingException
.
Similar to JUnit rules, the runner also reports stubbing argument mismatches as console warnings
(see MockitoHint
).
To opt-out from this feature, use @RunWith(MockitoJUnitRunner.Silent.class)
Mock
,
so that explicit usage of MockitoAnnotations.initMocks(Object)
is not necessary.
Mocks are initialized before each test method.
Mockito.validateMockitoUsage()
.
MockitoJUnitRunner.StrictStubs
variant of the runner.
It drives cleaner tests and improves debugging experience.
The only reason this feature is not turned on by default
is because it would have been an incompatible change
and Mockito strictly follows semantic versioning.
Mockito.validateMockitoUsage()
.
Read more about @Mock annotation in javadoc for MockitoAnnotations
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class ExampleTest {
@Mock
private List list;
@Test
public void shouldDoSomething() {
list.add(100);
}
}
If you would like to take advantage of Mockito JUnit runner features
but you cannot use the runner because, for example, you use TestNG, there is a solution!
MockitoSession
API is intended to offer cleaner tests and improved debuggability
to users that cannot use Mockito's built-in JUnit support (runner or the rule).Modifier and Type | Class and Description |
---|---|
static class |
MockitoJUnitRunner.Silent
This Mockito JUnit Runner implementation *ignores*
stubbing argument mismatches (
MockitoJUnitRunner.StrictStubs )
and *does not detect* unused stubbings. |
static class |
MockitoJUnitRunner.Strict
Detects unused stubs and reports them as failures.
|
static class |
MockitoJUnitRunner.StrictStubs
Improves debugging tests, helps keeping the tests clean.
|
Constructor and Description |
---|
MockitoJUnitRunner(Class<?> klass) |
Modifier and Type | Method and Description |
---|---|
void |
filter(Filter filter) |
Description |
getDescription() |
void |
run(RunNotifier notifier) |
public MockitoJUnitRunner(Class<?> klass) throws InvocationTargetException
InvocationTargetException
public void run(RunNotifier notifier)
public Description getDescription()
getDescription
in interface Describable
getDescription
in class Runner
public void filter(Filter filter) throws NoTestsRemainException
filter
in interface Filterable
NoTestsRemainException