Interface MockitoRule
- All Superinterfaces:
MethodRule
strictness(Strictness)
which automatically
detects stubbing argument mismatches and is planned to be the default in Mockito v3.
Since Mockito 2.1.0, JUnit rule emits stubbing warnings and hints to System output (see MockitoHint
).
The JUnit rule can be used instead of MockitoJUnitRunner
.
It requires JUnit at least 4.7.
The rule adds following behavior:
-
Since 2.1.0, stubbing warnings and hints are printed to System output.
Hints contain clickable links that take you right to the line of code that contains a possible problem.
Please give us feedback about the stubbing warnings of JUnit rules in the issue tracker
(issue 384).
It's a new feature of Mockito 2.1.0. It aims to help debugging tests.
If you wish the previous behavior, see
silent()
. However, we would really like to know why do you wish to silence the warnings! See alsoMockitoHint
. -
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 the rule with
strictness(Strictness)
configured toStrictness.STRICT_STUBS
. 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.
public class ExampleTest {
//Creating new rule with recommended Strictness setting
@Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
@Mock
private List list;
@Test
public void shouldDoSomething() {
list.add(100);
}
}
If you would like to take advantage of Mockito JUnit rule features
but you cannot use the rule 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).- Since:
- 1.10.17
-
Method Summary
Modifier and TypeMethodDescriptionsilent()
Rule will not report stubbing warnings during test execution.strictness
(Strictness strictness) The strictness, especially "strict stubs" (Strictness.STRICT_STUBS
) helps debugging and keeping tests clean.Methods inherited from interface org.junit.rules.MethodRule
apply
-
Method Details
-
silent
MockitoRule silent()Rule will not report stubbing warnings during test execution. By default, stubbing warnings are printed to Standard output to help debugging. Equivalent of configuringstrictness(Strictness)
withStrictness.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(); }
- Since:
- 2.1.0
-
strictness
The strictness, especially "strict stubs" (Strictness.STRICT_STUBS
) helps debugging and keeping tests clean. It's a new feature introduced in Mockito 2.3. Other levels of strictness - "warn" - (Strictness.WARN
) and "lenient" (silent()
) strictness were already present in Mockito 2.1.0. Version 2.3.0 introduces "strict stubs" (Strictness.STRICT_STUBS
).
See Javadoc forpublic class ExampleTest { @Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); }
Strictness
to learn how strictness influences the behavior of the JUnit rule. SeeStrictness.STRICT_STUBS
to learn why is it recommended to use "strict stubbing".It is possible to tweak the strictness per test method. Why would you need it? See the use cases in Javadoc for
PotentialStubbingProblem
class. In order to tweak strictness per stubbing seeMockito.lenient()
, per mock seeMockSettings.lenient()
.
"Strict stubs" are planned to be the default for Mockito v3 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 Mockito. Tell us what you think!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 } }
- Since:
- 2.3.0
-