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.

Overloaded method issues with strict

See original GitHub issue

When adding mocks for overloaded methods using Strict, Mockito throws an error that the wrong method was mocked.

mockito 2.22.0, JDK 1.8.0_181

package test;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class MockitoTest {

    @Test
    void validateStrictness() {
        MyInterface myInterface = mock(MyInterface.class);
        String expected1 = "oneArgument";
        String expected2 = "twoArguments";
        when(myInterface.getString(any())).thenReturn(expected1);
        when(myInterface.getString(any(), any())).thenReturn(expected2);

        String actual = myInterface.getString("one");
        assertEquals(actual, expected1);
        actual = myInterface.getString("one", "two");
        assertEquals(actual, expected2);
    }

    public interface MyInterface {
        String getString(String one);

        String getString(String one, String two);
    }
}
org.mockito.exceptions.misusing.PotentialStubbingProblem: 
Strict stubbing argument mismatch. Please check:
 - this invocation of 'getString' method:
    myInterface.getString(null, null);
    -> at test.MockitoTest.validateStrictness(MockitoTest.java:21)
 - has following stubbing(s) with different arguments:
    1. myInterface.getString(null);
      -> at test.MockitoTest.validateStrictness(MockitoTest.java:20)
Typically, stubbing argument mismatch indicates user mistake when writing tests.
Mockito fails early so that you can debug potential problem easily.
However, there are legit scenarios when this exception generates false negative signal:
  - stubbing the same method multiple times using 'given().will()' or 'when().then()' API
    Please use 'will().given()' or 'doReturn().when()' API for stubbing.
  - stubbed method is intentionally invoked with different arguments by code under test
    Please use default or 'silent' JUnit Rule (equivalent of Strictness.LENIENT).
For more information see javadoc for PotentialStubbingProblem class.

    at test.MockitoTest.validateStrictness(MockitoTest.java:21)


Process finished with exit code 255

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
DavidTannercommented, Sep 20, 2018

It looks like this has to do with calling when the second time. I couldn’t find what was going on, but the second when doesn’t attempt to add the mock logic, it just calls the method. Replacing with doReturn().when() works.


        doReturn(expected1).when(overloadedMethods).getString(any());
        doReturn(expected2).when(overloadedMethods).getString(any(), any());
1reaction
DavidTannercommented, Sep 20, 2018

Looking at the code, the issue appears to be from a naive stub comparison. The parameters of the method aren’t compared, just the name.

https://github.com/mockito/mockito/blob/v2.22.0/src/main/java/org/mockito/internal/junit/DefaultStubbingLookupListener.java#L59-L60

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to enforce strict checking for method overloading in ...
thx to Hassan Naqvi, I found the issue: to match my original code I should change the 3 first line to: const p1...
Read more >
fix definitions of overloaded methods : WI-19711 - YouTrack
Strict standards - fix definitions of overloaded methods · 1. Writing a method name, shows autocomplete + some shortcut inserts the complete method...
Read more >
5 Rules of Method Overloading and Overriding in Java ...
By the way, the right way to overload a method is to change the number of arguments or types of arguments because it...
Read more >
Guide to Overloading Methods in Java - Stack Abuse
Method overloading may improve code readability when you use it with care. In some instances, it even makes handling domain problems intuitive.
Read more >
Method overloading and null error in Java - GeeksforGeeks
The reason why we get compile time error in the above scenario is, here the method arguments Integer and String both are not...
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