org.mockito.junit
public interface MockitoRule extends MethodRule
MockitoHint
).
The JUnit rule can be used instead of MockitoJUnitRunner
.
It requires JUnit at least 4.7.
This rule adds following behavior:
silent()
.
However, we would really like to know why do you wish to silence the warnings!
See also MockitoHint
.
Mock
,
so that explicit usage of MockitoAnnotations.initMocks(Object)
is not necessary.
Mocks are initialized before each test method.
Mockito.validateMockitoUsage()
.
public class ExampleTest {
@Rule
public MockitoRule rule = MockitoJUnit.rule();
@Mock
private List list;
@Test
public void shouldDoSomething() {
list.add(100);
}
}
Modifier and Type | Method and Description |
---|---|
MockitoRule |
silent()
Rule will not report stubbing warnings during test execution.
|
MockitoRule |
strictness(Strictness strictness)
The strictness, especially "strict stubs" (
Strictness.STRICT_STUBS )
helps debugging and keeping tests clean. |
apply
MockitoRule silent()
strictness(Strictness)
with Strictness.LENIENT
.
Please give us feedback about the stubbing warnings of JUnit rules
by commenting on GitHub issue 769.
It's a new feature of Mockito 2.1.0. It aims to help debugging tests.
We want to make sure the feature is useful.
We would really like to know why do you wish to silence the warnings!
See also MockitoHint
.
Example:
public class ExampleTest {
@Rule
public MockitoRule rule = MockitoJUnit.rule().silent();
}
@Incubating MockitoRule strictness(Strictness strictness)
Strictness.STRICT_STUBS
)
helps debugging and keeping tests clean.
It's a new feature introduced in Mockito 2.3.
Other levels of strictness - "Warn" - current default (Strictness.WARN
)
and "lenient" (silent()
) strictness were already present in Mockito 2.1.0.
Version 2.3.0 introduces "strict stubs" (Strictness.STRICT_STUBS
).
public class ExampleTest {
@Rule
public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
}
How strictness level influences the behavior of JUnit rule:
Strictness.LENIENT
- equivalent of silent()
-
no added behavior. The default of Mockito 1.x Strictness.WARN
- helps keeping tests clean and with debuggability.
Reports console warnings about unused stubs
and stubbing argument mismatch (see MockitoHint
).
The default of Mockito 2.xStrictness.STRICT_STUBS
- ensures clean tests, reduces test code duplication, improves debuggability.
Adds following behavior:
PotentialStubbingProblem
).UnnecessaryStubbingException
).Mockito.verifyNoMoreInteractions(Object...)
you no longer need to explicitly verify stubbed invocations. They are automatically verified.PotentialStubbingProblem
class.
public class ExampleTest {
@Rule
public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
@Test public void exampleTest() {
//Change the strictness level only for this test method
//Useful for edge cases (see Javadoc for PotentialStubbingProblem class)
mockito.strictness(Strictness.LENIENT);
//remaining test code
}
}
"Strict stubs" are tentatively planned to be the default for Mockito 3.x
We are very eager to hear feedback about "strict stubbing" feature, let us know by commenting on GitHub
issue 769.
Strict stubbing is an attempt to improve testability and productivity with Mocktio. Tell us what you think!