org.mockito.exceptions.misusing
public class PotentialStubbingProblem extends MockitoException
MockitoRule.strictness(Strictness)
)
and JUnit Runner (MockitoJUnitRunner.StrictStubs
).
Detecting potential stubbing problems is intended to help writing and debugging tests.
The org.mockito.exceptions.misusing.PotentialStubbingProblem
exception is thrown
when mocked method is stubbed with some argument in test
but then invoked with different argument in code.
This scenario is called "stubbing argument mismatch".
Example:
//test method:
given(mock.getSomething(100)).willReturn(something);
//code under test:
Something something = mock.getSomething(50); // <-- stubbing argument mismatch
The stubbing argument mismatch is triggered in following use cases:
What to do if you fall into use case 3? You have 2 options:
BDDMockito.willReturn(Object)
or Mockito.doReturn(Object)
family of methods for stubbing.
Convenient stubbing via Mockito.when(Object)
has its drawbacks: the framework cannot distinguish between
actual invocation on mock (real code) and the stubbing declaration (test code).
Hence the need to use BDDMockito.willReturn(Object)
or Mockito.doReturn(Object)
for certain edge cases.
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:
mockito.strictness(Strictness.LENIENT);
//remaining test code
}
}
MockitoRule.strictness(Strictness)
with Strictness.STRICT_STUBS
for that test.
If you use JUnit Runner, don't use MockitoJUnitRunner.StrictStubs
for that test.
Strict stubbing will be unavailable for that test class.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!
Constructor and Description |
---|
PotentialStubbingProblem(String message) |
getUnfilteredStackTrace
fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
public PotentialStubbingProblem(String message)