-
Notifications
You must be signed in to change notification settings - Fork 41.6k
Consider @Primary annotation when using @MockBean #11066
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 #11066
Conversation
|
@alexandreBaronZnk Please sign the Contributor License Agreement! Click here to manually synchronize the status of this Pull Request. See the FAQ for frequently asked questions. |
|
@alexandreBaronZnk Thank you for signing the Contributor License Agreement! |
|
Thanks a lot for the patch @alexandreBaronZnk, I actually wanted to have a look into it right now myself. Let's hope it will be merged soon. Is there any chance that this bugfix (IMHO) is backported into 1.5.x as well? |
|
@alexandreBaronZnk Thanks for the PR. I was looking to see if we could merge this for M7 but I think there are a few things more that we need to think about. I'm not sure that we should change the Another item that we need to consider is that |
|
Hi @philwebb I created another pull request #11077 implementing your comment. I don't think that an extension of |
|
Hi @neiser, thanks you to improve the PR. But I have a remark : In review the code, I realize that MockitoPostProcessor manages not only mock bean but also spy bean. @Test
public void canSpyPrimaryBean() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
MockitoPostProcessor.register(context);
context.register(SpyPrimaryBean.class);
context.refresh();
assertThat(Mockito.mockingDetails(
context.getBean(MockQualifiedBean.class).mock)
.isSpy()).isTrue();
assertThat(Mockito.mockingDetails(
context.getBean(ExampleService.class))
.isSpy()).isFalse();
assertThat(Mockito.mockingDetails(
context.getBean("examplePrimary", ExampleService.class))
.isSpy()).isFalse();
assertThat(Mockito.mockingDetails(
context.getBean("exampleQualified", ExampleService.class))
.isSpy()).isTrue();
}
@Configuration
static class SpyPrimaryBean {
@SpyBean(ExampleService.class)
private ExampleService spy;
@Bean
@Qualifier("test")
public ExampleService exampleQualified() {
return new RealExampleService("qualified");
}
@Bean
@Primary
public ExampleService examplePrimary() {
return new RealExampleService("primary");
}
}And I got an exception like those before the PR with mock bean annotation I think the behavior between mock and spy bean must be identical. |
|
Hi @alexandreBaronZnk the code for |
|
I added your test and fixed it (you were still using |
|
I think this PR can be closed in favor of #11077 |
#10655
Consider Primary annotation when choose bean candidates and add primary flag to mock bean