Overloaded method issues with strict
See original GitHub issueWhen 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:
- Created 5 years ago
- Reactions:3
- Comments:13 (3 by maintainers)
Top 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 >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
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.
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