-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Spy doesn't forward hashcode/equals to actual object #2331
Comments
To create a mock a subclass of a mock is created through SubclassBytecodeGenerator.mockClass() here we can see that hashCode method is intercepted and handled by MockMethodInterceptor.ForHashCode. Here the code returns System.identityHashCode(thiz) as the hashcode where thiz is the object returned by Mockito.spy(). Same is the case of equals, where this==that is returned when equals is called. @TimvdLippe shoud we add handling to delegate the call to hashCode and equals of spied object ? |
Hm, I guess we could? I wonder if any tests start failing after that, but if they all pass I am okay with making that change yeah 👍 |
Has conditionally on basis of whether default answer is pass to actual methods made hash code and equals pass to actual methods.
Has conditionally on basis of whether default answer is pass to actual methods made hash code and equals pass to actual methods. Intellij seems to had only pushed import and not the test
I have opened a pull request here. |
After the fix in mockito/mockito#2331, the calls to equals on the fake transfer listener (due to its use in a list of listeners) are treated as interactions with it, meaning that the current verification of 'no more interactions' will fail. This change makes the transfer listener used for testing count bytes then delegate to another (mock) transfer listener that's passed in to avoid the problem. PiperOrigin-RevId: 391949619
*** Original commit *** Avoid adding spy to list in DataSourceContractTests After the fix in mockito/mockito#2331, the calls to equals on the fake transfer listener (due to its use in a list of listeners) are treated as interactions with it, meaning that the current verification of 'no more interactions' will fail. This change makes the transfer listener used for testing count bytes then delegate to another (mock) transfer listener that's passed in to avoid the problem. *** PiperOrigin-RevId: 393093785
We use the same generated classes for both spy and mock classes. This is why we had to unify the behaviour. Internally, a spy is nothing but a mock with a prefefined delegatory answer. This is why a spy needs to behave like a mock. Generally, I agree with that this behaviour would to be preferred. But it would require a lot of internal refactoring as we often rely on the hashcode/equals properties of mocks. |
Stumbled upon this today. If this cannot be fixed in the near future I would suggest to at least document this e.g. in de JavaDoc of |
Mockito 3.11.1
Note that some configuration are impossible to mock via Mockito
(same as any question on stackoverflow.com)
The text was updated successfully, but these errors were encountered: