public class MockitoJUnitRunner
extends org.junit.runner.Runner
implements org.junit.runner.manipulation.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.openMocks(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 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(org.junit.runner.manipulation.Filter filter) |
org.junit.runner.Description |
getDescription() |
void |
run(org.junit.runner.notification.RunNotifier notifier) |
public MockitoJUnitRunner(Class<?> klass) throws InvocationTargetException
InvocationTargetException
public void run(org.junit.runner.notification.RunNotifier notifier)
run
in class org.junit.runner.Runner
public org.junit.runner.Description getDescription()
getDescription
in interface org.junit.runner.Describable
getDescription
in class org.junit.runner.Runner
public void filter(org.junit.runner.manipulation.Filter filter) throws org.junit.runner.manipulation.NoTestsRemainException
filter
in interface org.junit.runner.manipulation.Filterable
org.junit.runner.manipulation.NoTestsRemainException