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
Overloaded method issues with strict #1496
Comments
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. |
Digging deeper this is being caused because I am trying to mock the overloaded method. |
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()); |
I ran into this issue today, with the following two stubs:
The documentation is clear that this is okay. But I am getting the "Strict stubbing argument mismatch." error with Mockito 2.22.x. |
In reference to @DavidTanner comment #1496 (comment), I have said the same in #1353. I had a look at it and tried to modify the logic of comparison to include the parameters as well, so my final outcome was like this: private List<Invocation> potentialArgMismatches(Invocation invocation) {
List<Invocation> matchingStubbings = new LinkedList<Invocation>();
Collection<Stubbing> stubbings = mockingDetails(invocation.getMock()).getStubbings();
for (Stubbing s : stubbings) {
if (!s.wasUsed() && s.getInvocation().getMethod().equals(invocation.getMethod())) {
matchingStubbings.add(s.getInvocation());
}
}
return matchingStubbings;
} I've done this about 1 year and half ago with mockito release 2.16.0. If I recall correctly I had some tests setup to check that The documentation of
|
Same problem here. This is quite a nuisance. Since this has been open for quite a while already: are there any plans to change that? |
This should have been fixed in #1539 but was not automatically closed. Are you still able to reproduce this issue on Mockito 3.3.9? |
Hi! I am using Mockito-Junit-Jupiter 3.3.3, and made sure that it also uses Mockito-Core 3.3.3 underneath, but the problem is still there. |
No 3.3.9 is the latest version that is available on Bintray: https://bintray.com/mockito/maven/mockito-development. But 3.3.3 is recent enough as well. Please open a PR with a test case added to our test suite that shows the problem and we can take a look. CC @mockitoguy |
Just noticed version 3.3.9 on bintray is mockito-android. |
I have created a very simple project setup that reproduces the issues: Contains 5 Tests, out of which 3 are failing. Would be great if you could have a look at this. Maybe I am doing it wrong? |
Which ones are failing and which ones aren't? Please annotate the tests if they are failing and on which line. Reading the tests, I would expect the following: |
Sorry, my bad, So in short the solution is to use the @ExtendWith(MockitoExtension.class)
public class Test1 {
@Mock
Token token;
@Mock
OAuth2TokenClient tokenClient;
@Test
void testFunctionOverloading() throws MalformedURLException {
when(tokenClient.fetchToken(any(URL.class), anyString(), anyString(), (Duration) ArgumentMatchers.isNull())).thenReturn(token);
TokenProvider provider = new TokenProvider(tokenClient);
provider.getToken(URI.create("http://test.com").toURL(), "username", "password");
}
} |
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
The text was updated successfully, but these errors were encountered: