Visitar URL original
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. by vyazelenko · Pull Request #2400 · mockito/mockito · GitHub
Skip to content

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
TimvdLippe merged 2 commits into
mockito:mainfrom
vyazelenko:main
Aug 24, 2021

Conversation

@vyazelenko

Copy link
Copy Markdown
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.

@TimvdLippe TimvdLippe left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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

@vyazelenko

Copy link
Copy Markdown
Contributor Author

@TimvdLippe Pushed formatting fixes.

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

raphw commented Aug 24, 2021 •

Copy link
Copy Markdown
Member

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
Copy Markdown
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.

3 participants