-
Notifications
You must be signed in to change notification settings - Fork 40.6k
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
Consider @Primary annotation when using @MockBean, make @MockBean/@SpyBean behave consistently #11077
Conversation
@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. |
@neiser Thank you for signing the Contributor License Agreement! |
@philwebb Can you explain why the handling of bean registration is so different for Note that I didn't change anything in the current behaviour for 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 |
@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 |
Any update on this? |
@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 Any update on this? Is there an estimate when this is going to be reviewed? |
@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. |
@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 |
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>
Add additional tests to ensure that `@MockBean` and `@SpyBean` work consistently when combined with `@Primary`. See gh-11077
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! |
I realized your |
@philwebb Thanks a lot for merging it! |
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 infindCandidateBeans
first).I cannot push to @alexandreBaronZnk's branch, I hope it's okay that I created another pull request.