Package org.mockito.junit
Class MockitoJUnitRunner
- java.lang.Object
-
- org.junit.runner.Runner
-
- org.mockito.junit.MockitoJUnitRunner
-
- All Implemented Interfaces:
Describable
,Filterable
- Direct Known Subclasses:
MockitoJUnitRunner.Silent
,MockitoJUnitRunner.Strict
,MockitoJUnitRunner.StrictStubs
public class MockitoJUnitRunner extends Runner implements Filterable
Mockito JUnit Runner keeps tests clean and improves debugging experience. Make sure to try outMockitoJUnitRunner.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
If you would like to take advantage of Mockito JUnit runner features, but you cannot use the runner there is a solution!@RunWith(MockitoJUnitRunner.StrictStubs.class) public class ExampleTest { @Mock private List list; @Test public void shouldDoSomething() { list.add(100); } }
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 Classes Modifier and Type Class 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 Summary
Constructors Constructor Description MockitoJUnitRunner(Class<?> klass)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
filter(Filter filter)
Description
getDescription()
void
run(RunNotifier notifier)
-
-
-
Constructor Detail
-
MockitoJUnitRunner
public MockitoJUnitRunner(Class<?> klass) throws InvocationTargetException
- Throws:
InvocationTargetException
-
-
Method Detail
-
run
public void run(RunNotifier notifier)
-
getDescription
public Description getDescription()
- Specified by:
getDescription
in interfaceDescribable
- Specified by:
getDescription
in classRunner
-
filter
public void filter(Filter filter) throws NoTestsRemainException
- Specified by:
filter
in interfaceFilterable
- Throws:
NoTestsRemainException
-
-