Skip to content
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

Fixes #2399 : Adds defaultAnswer to the MockitoMockKey to distinguish the mock types, i.e. to separate mocks from spies otherwise spy type is reused for a mock or vice versa. #2400

Merged
merged 2 commits into from Aug 24, 2021

Conversation

vyazelenko
Copy link
Contributor

Checklist

  • Read the contributing guide
  • PR should be motivated, i.e. what does it fix, why, and if relevant how
  • If possible / relevant include an example in the description, that could help all readers
    including project members to get a better picture of the change
  • Avoid other runtime dependencies
  • Meaningful commit history ; intention is important please rebase your commit history so that each
    commit is meaningful and help the people that will explore a change in 2 years
  • The pull request follows coding style
  • Mention Fixes #<issue number> in the description if relevant
  • At least one commit should mention Fixes #<issue number> if relevant

…inguish the mock types, i.e. to separate mocks from spies otherwise spy type is reused for a mock or vice versa.
Copy link
Contributor

@TimvdLippe TimvdLippe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find, fix and tests! 🎉

@TimvdLippe
Copy link
Contributor

Ah, you will need to run ./gradlew spotlessApply locally to fix the formatting issues.

@vyazelenko
Copy link
Contributor Author

@TimvdLippe Pushed formatting fixes.

@TimvdLippe TimvdLippe merged commit 4f81d4f into mockito:main Aug 24, 2021
@raphw
Copy link
Member

raphw commented Aug 24, 2021

That's a bad patch, I'm afraid. Answer is an interface and most instances do not implement hashCode/equals, this effectively breaks the caching mechanism what can cause significant overhead. The problem is the fix for #2331 which we must roll back. Mocks and spies classes must not consider the default answer when created classes. This must be solved on the interceptor level. I see what I can do but generally, we should not have allowed for the exception on the object methods.

I also see that this created an inconsistency for the inline and subclass mock makers. I am not entirely sure why this restriction was ever introduced in Mockito, but if we want to remove it, we would need to remove it generally, not for spies only.

@TimvdLippe
Copy link
Contributor

Oof, that's definitely on me for not fully understanding the domain space. Thanks Rafael for the additional knowledge, that was not something I was aware of. Reverting all the fixes seems like the right course of action at this point in time, to avoid further snowballing.

I still think the intent of #2331 is good (it caught numerous bad tests at Google), so I would like to figure out an alternative solution that does not have the downsides of the existing implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants