Misleading UnnecessaryStubbingException when Mockito is misused
See original GitHub issueIn a certain case mockito fails to report a missing method on a mock instruction:
when(ormSupport).thenReturn(null);
The reproducible test can be found here:
Mockito fails the test with the following error message which is completely misleading:
org.mockito.exceptions.misusing.UnnecessaryStubbingException:
Unnecessary stubbings detected in test class: MockitoProblem
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
1. -> at ch.mgysel.mockito.MainComponent.<init>(MainComponent.java:10)
Please remove unnecessary stubbings or use 'silent' option. More info: javadoc for UnnecessaryStubbingException class.
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:49)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:142)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
I would expect Mockito to fail the test with the following message:
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
at ch.mgysel.mockito.MockitoProblem.test(MockitoProblem.java:31)
This is works then I remove the setUp()
from the test.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:13 (4 by maintainers)
Top Results From Across the Web
How to resolve Unneccessary Stubbing exception
At first you should check your test logic. Usually there are 3 cases. First, you are mocking the wrong method (you made a...
Read more >Mockito Strict Stubbing and The UnnecessaryStubbingException
This exception is a common exception we'll likely encounter when using stubs incorrectly. We'll start by explaining the philosophy behind strict ...
Read more >UnnecessaryStubbingException (Mockito 3.1.0 API) - javadoc.io
This exception indicates presence of unused stubbings. It is highly recommended to remove unused stubbings to keep the codebase clean.
Read more >Mockito (Mockito 3.9.0 API) - Javadoc
For example 0 for an int/Integer and false for a boolean/Boolean. ... in his @After method so that he knows immediately when he...
Read more >src/main/java/org/mockito/internal/exceptions/Reporter.java
import org.mockito.exceptions.misusing.*; ... "Incorrect use of API detected here:", ... More info: javadoc for UnnecessaryStubbingException class.".
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have a scenario where I have a single test class with 4 test methods. I have a @BeforeEach method where I am doing 2 stubbings using doReturn().when()…
Of the 4 tests in the class, 3 of them need both the stubbings, but the remaining 1 test needs only 1 of the 2 stubbings. When I run the test, I get an UnnecessaryStubbingException for that one test which does not need both the stubbings.
As per the the documentation of the UnnecessaryStubbingException class - “This means that it is ok to put default stubbing in a ‘setup’ method or in test class constructor. That default stubbing needs to be used at least once by one of the test methods.”. However, I dont see it working that way. Can somebody clarify? Thanks.
@TimvdLippe On few occasions I saw stubbing done in @Before function for all tests. Therefore, the unnecessary stubbing may be for one test but not the other. Blindly removing those stubs can potentially have bad impact on tests that were admittedly already not perfect. It is a pity that the unnecessary stubbing is not reported per test as this will allow to see if it makes sense to refactor tests and move stubbing from @Before to the test itself.