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

Make @SpringMock work for beans with @Primary #1503

Conversation

konradczajka
Copy link
Contributor

fixes #1502

When the context contains multiple beans for given interface
but one of them is marked as @primary it should be possible
to mock this interface.

Current implementation doesn't check if one of found beans is primary
and fails with an exception.

My change mirrors a similar logic used in spring-boot-starter-test
and resolves this issue.

fixes spockframework#1502

When the context contains multiple beans for given interface
but one of them is marked as @primary it should be possible
to mock this interface.

Current implementation doesn't check if one of found beans is primary
and fails with an exception.

My change mirrors a similar logic used in spring-boot-starter-test
and resolves this issue.
@konradczajka konradczajka changed the title Make @SpringMock work for beans with multiple instances Make @SpringMock work for beans with @Primary Jul 26, 2022
@leonard84
Copy link
Member

Could you also check if there are other relevant new additions that were made to MockitoPostProcessor that are missing in SpockMockPostProcessor?

…nces

Additional assertions in new tests are commented as depepnding on
whether tests are run with :test or :testCglib they either pass
or fail with "org.opentest4j.MultipleFailuresError: Multiple Failures (2 failures)"
exception - one failure being CannotCreateMockException
and the other one the actual context loading problem.
@konradczajka
Copy link
Contributor Author

konradczajka commented Jul 30, 2022

Could you also check if there are other relevant new additions that were made to MockitoPostProcessor that are missing in SpockMockPostProcessor?

I found four bugfixes applied to the MockitoPostProcessor that seems to be missing in the SpockMockPostProcessor. One of them had been reverted later as it caused other problems. That leaves those three:

  • 29639, which can and probably should be ported
  • 27693, which probably is not important as @Repeat is not used in Spock's specifications and other ways of running given test multiple times (e.g. @Unroll("#i") with where: i << (1..10)) don't seem to be affected by this problem.
  • 20665, which I was unable to reproduce with Spock. Mostly because @SpringBean cannot be applied in the @Configuration classes (using DetachedMockFactory doesn't trigger the bug).

Apart from them there were ~4 PRs with refactorings/code polishing.

I'll port the fix for 29639 here.

@konradczajka konradczajka force-pushed the mocking-bean-with-multiple-instances branch from 6bc8f6c to ddd2b4c Compare July 30, 2022 10:34
@leonard84 leonard84 self-assigned this Aug 9, 2022
@leonard84 leonard84 added this to the 2.2 milestone Aug 9, 2022
@konradczajka
Copy link
Contributor Author

Hi @leonard84
Is there anything else you'd like to be changed in this PR or can it be merged?

@codecov
Copy link

codecov bot commented Aug 20, 2022

Codecov Report

Merging #1503 (27bc768) into master (016c948) will increase coverage by 0.04%.
The diff coverage is 85.71%.

@@             Coverage Diff              @@
##             master    #1503      +/-   ##
============================================
+ Coverage     79.76%   79.81%   +0.04%     
- Complexity     4047     4051       +4     
============================================
  Files           412      412              
  Lines         12779    12795      +16     
  Branches       1652     1656       +4     
============================================
+ Hits          10193    10212      +19     
+ Misses         1988     1985       -3     
  Partials        598      598              
Impacted Files Coverage Δ
...kframework/spring/mock/SpockMockPostprocessor.java 88.34% <85.71%> (+3.30%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@leonard84 leonard84 left a comment

Choose a reason for hiding this comment

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

Thanks for the fix @konradczajka

@leonard84 leonard84 merged commit 6772caa into spockframework:master Aug 20, 2022
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.

Unable to mock a Spring primary bean when other instances are available
2 participants