spock icon indicating copy to clipboard operation
spock copied to clipboard

Make @SpringMock work for beans with @Primary

Open konradczajka opened this issue 3 years ago • 2 comments

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.

konradczajka avatar Jul 26 '22 19:07 konradczajka

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

leonard84 avatar Jul 28 '22 18:07 leonard84

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 avatar Jul 30 '22 08:07 konradczajka

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

konradczajka avatar Aug 15 '22 16:08 konradczajka

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%) :arrow_up:

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

codecov[bot] avatar Aug 20 '22 16:08 codecov[bot]