New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Misleading UnnecessaryStubbingException when Mockito is misused #873
Comments
Hi I can't reproduce the issue with recent mockito versions. Just to be sure I also checked the same version of your project : Mockito 2.5.5. And Mockito behaves correctly. |
@bric3, can we please reopen this one? I can still reproduce it with Mockito 2.5.5 as well as with 2.5.6... clone gysel/mockito-problem and run
I expect Mockito to throw a Please note that this works in other cases. However the particular |
OK I'll give it another look. |
I was able to reproduce the problem. @gysel, thank you very much for fantastic report and super easy way to reproduce the issue. In this case, we cannot really do much. Mockito syntax is clean but it has trade-offs. Example: mock1.foo();
//below, Mockito thinks that you're stubbing mock1.foo() method:
when(mock2).thenReturn(null); In above example, Mockito is not aware that there is incorrect stubbing. doReturn() api for stubbing does not have this problem. When/If we change the syntax in the future (Mockito 3), we should be able to avoid this caveat. I think there are 2 actionable TODOs:
Thanks again for reporting! If you have ideas how to improve this scenario better, we would love to hear them. |
Why are these UnnecessaryStubbingException's happening? After upgrading from mockito-core-1.9.5 to mockito-core-2.9.0, tons of the exceptions occured. After removing the stubbings, the tests still run fine and show coverage and what not, but I fear some test's meaningfulness has been lost. And is it really safe to just blindly remove these unnecessary stubs? This is difficult to introspect when all the tests are passing fine. Thanks in advance for your insights. |
@bradleywillard If a stubbing was defined but never invoked, the exception is thrown. As such, you can safely delete these stubbings and still have your tests pass. Hopefully that makes it a bit more clear! |
@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. |
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. |
I see that mockito 2.23.4 thinks wrong stubbing actually invoked. |
Removing @ExtendWith(MockitoExtension.class) annonation and adding MockitoAnnotations.initMocks(this) under init setup i.e using @before solves this problem.It worked for me |
Thanks for the workaround, but I think that just disables the check for unnecessary stubbings. Not a real solution. |
@novicedev7291 the best workaround I can find is to add This seems to be a difference between the |
In 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:
https://github.com/gysel/mockito-problem/blob/master/src/test/java/ch/mgysel/mockito/MockitoProblem.java
Mockito fails the test with the following error message which is completely misleading:
I would expect Mockito to fail the test with the following message:
This is works then I remove the
setUp()
from the test.The text was updated successfully, but these errors were encountered: