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

Consider @Primary annotation when using @MockBean, make @MockBean/@SpyBean behave consistently #11077

Closed

Conversation

neiser
Copy link
Contributor

@neiser neiser commented Nov 18, 2017

The discussion in #11066 revealed that the proposed solution is not ideal. Based on @alexandreBaronZnk's work, I created another pull request and tried to honor @philwebb's remarks.

I changed that getBeanName respects the @Primary annotation (and does not simply fail if more than one candidate is found) and I copied the primary flag from the removed bean to the mocked bean. This makes the tests work again (of course I removed the changes in findCandidateBeans first).

I cannot push to @alexandreBaronZnk's branch, I hope it's okay that I created another pull request.

@pivotal-issuemaster
Copy link

@neiser Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@pivotal-issuemaster
Copy link

@neiser Thank you for signing the Contributor License Agreement!

@neiser
Copy link
Contributor Author

neiser commented Nov 18, 2017

@philwebb Can you explain why the handling of bean registration is so different for @MockBean and @SpyBean in MockitoPostProcessor? I added some TODO comments in the last commit, as the analogous test where you have a primary bean but want to spy a qualified one does not work.

Note that I didn't change anything in the current behaviour for @SpyBean, so it already had some handling for the @Primary annotation in MockitoPostProcessor.determinePrimaryCandidate, which is only used when registering spies (and not mocks).

This is all quite mysterious to me. I hope you can shed some light on it.

Update: I added also an integration test illustrating the problem with @SpyBean.

@philwebb
Copy link
Member

@neiser Spy beans are quite tricky. With standard mocks we can replace the bean definition early and just create a mock instance when the bean is needed. For spy beans we need to allow the standard instance to be created, then wrap it with a spy.

In registerSpy we actually just keep track of the definitions. We then have a SpyPostProcessor class that gets called after every bean is created. It delegates back to MockitoPostProcessor.createSpyIfNecessary to actually wrap the real bean instance in a spy if we know that's what the user wants.

@philwebb philwebb self-assigned this Feb 2, 2018
@philwebb philwebb added this to the 2.0.0.RC2 milestone Feb 2, 2018
@philwebb philwebb modified the milestones: 2.0.0.RC2, 2.0.0 Feb 21, 2018
@snicoll snicoll modified the milestones: 2.0.0, 2.1.0 Feb 21, 2018
@bopfermann
Copy link

Any update on this?

@neiser
Copy link
Contributor Author

neiser commented Mar 22, 2018

@snicoll @philwebb I'm happy to resolve the merge conflicts which have appeared by now. I'd just like to know if there's any chance an updated PR will be reviewed after some reasonable time.

@philwebb
Copy link
Member

@neiser Sorry this didn't make it into 2.0, we just ran out of time. I'll add it to the 2.1 backlog. There's no need to rebase, we can do that when we look at merging it.

@philwebb philwebb modified the milestones: Icebox, Backlog Mar 27, 2018
@neiser
Copy link
Contributor Author

neiser commented May 24, 2018

@philwebb Any update on this? Is there an estimate when this is going to be reviewed?

@philwebb
Copy link
Member

@neiser It's on the backlog which means we'd like to include it in the next release but I'm afraid I can't give you an estimate for when it might be reviewed.

@neiser
Copy link
Contributor Author

neiser commented Aug 24, 2018

@philwebb We're approaching now one year that this PR is open. Any chance this gets reviewed soon? I'd also consider this more a bug than an enhancement by the way, as using @Primary with @MockBean is indeed broken (though I haven't checked the most recent Spring Boot release).

philwebb added a commit that referenced this pull request Sep 6, 2018
Remove factory method previously used to create the mock instance.
Since commit 0e00a49, the method is not longer needed.

See gh-11077
philwebb added a commit that referenced this pull request Sep 6, 2018
Update `MockitoPostProcessor` so that `@MockBean` and `@SpyBean`
work consistently when combined with `@Primary`.

See gh-11077

Co-authored-by: Andreas Neiser <andreas.neiser@gmail.com>
philwebb pushed a commit that referenced this pull request Sep 6, 2018
Add additional tests to ensure that `@MockBean` and `@SpyBean`
work consistently when combined with `@Primary`.

See gh-11077
@philwebb philwebb closed this in bc35722 Sep 6, 2018
philwebb added a commit that referenced this pull request Sep 6, 2018
@philwebb
Copy link
Member

philwebb commented Sep 6, 2018

Thanks for bearing with us on this one @neiser! I've now pushed a version of it to master so it should be in 2.1. Unfortunately the PR didn't apply that cleanly so I've chopped it up a bit. The tests from your original PR (with some slight formatting changes) are in a5b3a26. The main code changes are in c777614 but they've been made directly to the post processor rather than introducing a new class.

Thanks again!

@philwebb
Copy link
Member

philwebb commented Sep 6, 2018

I realized your @Author attribution also got lost in the mix. I've just fixed that in 0471037.

@wilkinsona wilkinsona modified the milestones: 2.1.x, 2.1.0.M3 Sep 7, 2018
@neiser neiser deleted the consider-primary-when-mock-bean branch September 8, 2018 05:47
@neiser
Copy link
Contributor Author

neiser commented Sep 8, 2018

@philwebb Thanks a lot for merging it!

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

Successfully merging this pull request may close these issues.

9 participants