Broken unused stubbing reporting when matching previous stubbed call
See original GitHub issueWhen stubbing the same method call twice, and the second stub setup matches to first stubbed call, the first stub is not reported as unused, because Mockito detects the next stub setup as a usage of the previous stub setup. This is an example test:
public class UnnecessaryStubbingWithImplicitEqMatchers {
@Mock IMethods mock;
MockitoSession mockito = Mockito.mockitoSession().initMocks(this).strictness(Strictness.STRICT_STUBS).startMocking();
@After public void after() {
mockito.finishMocking();
}
@Test public void unnecessary_stubbing() {
given(mock.simpleMethod("1")).willReturn("one");
given(mock.simpleMethod("2")).willReturn("three");
given(mock.simpleMethod("1")).willReturn("two");
given(mock.simpleMethod("1")).willReturn("four");
mock.simpleMethod("1");
mock.simpleMethod("2");
}
}
The test above should fail with UnnecessaryStubbingException
containing two unused stubs, but it does not.
Mockito version: v3.2.4
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Mockito Strict Stubbing and The UnnecessaryStubbingException
Simply put, an unnecessary stub is a stubbed method call that was never realized during test execution. Let's take a look at a...
Read more >How to resolve Unneccessary Stubbing exception
First, you are mocking the wrong method (you made a typo or someone changed tested code so that mocked method is no longer...
Read more >New lenient() strictness setting available at mock and ... - GitHub
As we use this method at many places, where different configuration is needed, it will cause a lot of unnecessary stubbing. Thus, we...
Read more >Mockito (Mockito 4.9.0 API) - javadoc.io
Once stubbed, the method will always return a stubbed value, regardless of how many times it is called. Last stubbing is more important...
Read more >Mockito gotcha: Beware of method invocation when stubbing
Stub it to use the result of answerSizeOfInputPlus(2) when called. ... matching the current ones — the method returns that stubbed value.
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
Revert in progress: https://github.com/mockito/mockito/pull/1878
@pkoenig10 I fail to see how that is less verbose than multiple stubbings, but in any case, that’s not the point. I think this change should be reverted until it can be given a deeper thought. do you agree? @mockito/developers ?