PotentialStubbingProblem is a RuntimeException and subclass of MockitoException. Mockito framework throws this exception when “stubbing arguments mismatch“.
Strict stubbing is a new opt-in feature for JUnit Rule and JUnit Runner to detect potential stubbing problems 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 exception can occurred by many reasons:
- Mistake or typo in the test code, the argument(s) used when declaring stubbing’s is unintentionally different.
- Mistake or typo in the code under test, the argument(s) used in the code under test is unintentionally different.
- Intentional use of stubbed method with different argument, either in the test (more stubbing) or in code under test.
Constructors
- PotentialStubbingProblem(String message) : Will throw exception with message.
Example
//test method: given(mock.getSomething(200)).willReturn(something); //code under test: // stubbing argument mismatch Something something = mock.getSomething(100);
Solutions
public class TestExample { @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 given(mock.getSomething(200)).willReturn(something); //Will work Something something = mock.getSomething(100); } }
References
Know More
To know more about Junit, Mockito and exception solutions follow below links:
You must log in to post a comment.