question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Broken unused stubbing reporting when matching previous stubbed call

See original GitHub issue

When 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:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
TimvdLippecommented, Feb 26, 2020
0reactions
bbonannocommented, Feb 24, 2020

@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 ?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found