Package org.mockito.junit
Class MockitoJUnitRunner
java.lang.Object
org.junit.runner.Runner
org.mockito.junit.MockitoJUnitRunner
- All Implemented Interfaces:
org.junit.runner.Describable
,org.junit.runner.manipulation.Filterable
- Direct Known Subclasses:
MockitoJUnitRunner
,MockitoJUnitRunner.Silent
,MockitoJUnitRunner.Strict
,MockitoJUnitRunner.StrictStubs
public class MockitoJUnitRunner
extends org.junit.runner.Runner
implements org.junit.runner.manipulation.Filterable
Mockito JUnit Runner keeps tests clean and improves debugging experience.
Make sure to try out
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:
-
(new since Mockito 2.1.0) Detects unused stubs in the test code.
See
UnnecessaryStubbingException
. Similar to JUnit rules, the runner also reports stubbing argument mismatches as console warnings (seeMockitoHint
). To opt-out from this feature, use @RunWith(MockitoJUnitRunner.Silent.class) -
Initializes mocks annotated with
Mock
, so that explicit usage ofMockitoAnnotations.openMocks(Object)
is not necessary. Mocks are initialized before each test method. -
Validates framework usage after each test method. See javadoc for
Mockito.validateMockitoUsage()
. -
It is highly recommended to use
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).-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
This Mockito JUnit Runner implementation *ignores* stubbing argument mismatches (MockitoJUnitRunner.StrictStubs
) and *does not detect* unused stubbings.static class
Detects unused stubs and reports them as failures.static class
Improves debugging tests, helps keeping the tests clean. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionMockitoJUnitRunner
(Class<?> klass) MockitoJUnitRunner
(InternalRunner runner) -
Method Summary
Methods inherited from class org.junit.runner.Runner
testCount
-
Field Details
-
runner
-
-
Constructor Details
-
MockitoJUnitRunner
- Throws:
InvocationTargetException
-
MockitoJUnitRunner
MockitoJUnitRunner(InternalRunner runner) throws InvocationTargetException - Throws:
InvocationTargetException
-
-
Method Details
-
run
public void run(org.junit.runner.notification.RunNotifier notifier) - Specified by:
run
in classorg.junit.runner.Runner
-
getDescription
public org.junit.runner.Description getDescription()- Specified by:
getDescription
in interfaceorg.junit.runner.Describable
- Specified by:
getDescription
in classorg.junit.runner.Runner
-
filter
public void filter(org.junit.runner.manipulation.Filter filter) throws org.junit.runner.manipulation.NoTestsRemainException - Specified by:
filter
in interfaceorg.junit.runner.manipulation.Filterable
- Throws:
org.junit.runner.manipulation.NoTestsRemainException
-