public class PotentialStubbingProblem extends MockitoException
PotentialStubbingProblem
improves productivity by failing the test early when the user
misconfigures mock's stubbing.
PotentialStubbingProblem
exception is a part of "strict stubbing" Mockito API
intended to drive cleaner tests and better productivity with Mockito mocks.
For more information see Strictness
.
PotentialStubbingProblem
is thrown when mocked method is stubbed with some argument in test
but then invoked with different argument in the 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 typically indicates:
PotentialStubbingProblem
improves productivity in those scenarios
by failing early with clean message pointing out the incorrect stubbing or incorrect invocation of stubbed method.
In remaining 5% of the cases (use case 3) PotentialStubbingProblem
can give false negative signal
indicating non-existing problem. The exception message contains information how to opt-out from the feature.
Mockito optimizes for enhanced productivity of 95% of the cases while offering opt-out for remaining 5%.
False negative signal for edge cases is a trade-off for general improvement of productivity.
What to do if you fall into use case 3 (false negative signal)? 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.
It is a well known limitation of Mockito API and another example how Mockito optimizes its clean API for 95% of the cases
while still supporting edge cases.
Mockito.lenient()
Mockito team is 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!
Constructor and Description |
---|
PotentialStubbingProblem(String message) |
getUnfilteredStackTrace
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
public PotentialStubbingProblem(String message)