Skip to content

Avoid spying collection-like types with @Spy if it may mislead @InjectMocks behaviour #2642

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

Conversation

tepa46
Copy link
Collaborator

@tepa46 tepa46 commented Oct 4, 2023

Description

If we have models of different types implementing Collection or Map, we should not construct fields of these models with @Spy annotation but just use setField for it. Otherwise it will cause problems with @InjectMocks in Mockito because injection by type is used and in generated tests both List<A> and List<B> are represented as List listSpy.

One of the possible scenarios to reproduce the bug we fix here is to generate tests for Owner class in spring-petclinic project with 100% fuzzing. Before this fix, several different spies were created but injected not as we expect after that.

Self-check list

  • I've set the proper labels for my PR (at least, for category and component).
  • PR title and description are clear and intelligible.
  • I've added enough comments to my code, particularly in hard-to-understand areas.
  • The functionality I've repaired, changed or added is covered with automated tests.
  • Manual tests have been provided optionally.
  • The documentation for the functionality I've been working on is up-to-date.

@tepa46 tepa46 added comp-codegen Issue is related to code generator ctg-bug-fix PR is fixing a bug labels Oct 4, 2023
@tepa46 tepa46 changed the title Fix bug with injecting of different types of thisInstance fields. Fix bug with injecting of different types of thisInstance fields Oct 4, 2023
@EgorkaKulikov EgorkaKulikov changed the title Fix bug with injecting of different types of thisInstance fields Fix bug with spying different collection-like types of thisInstance fields Oct 4, 2023
@EgorkaKulikov EgorkaKulikov changed the title Fix bug with spying different collection-like types of thisInstance fields Avoid spying collection-like types with @Spy if it may mislead @InjectMocks behaviour Oct 4, 2023
Copy link
Collaborator

@IlyaMuravjov IlyaMuravjov left a comment

Choose a reason for hiding this comment

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

Mostly LGTM

@alisevych alisevych merged commit 14fc1ea into main Oct 4, 2023
@alisevych alisevych deleted the kirill/fix-bug-with-Spying-different-types-of-thisInstance-fields branch October 4, 2023 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-codegen Issue is related to code generator ctg-bug-fix PR is fixing a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants