Spring-boot: Support primary beans with ApplicationContextAssert.getBean

Created on 17 Oct 2018  路  2Comments  路  Source: spring-projects/spring-boot

When having multiple beans defined with one marked with @Primary I'd expect that ApplicationContextAssert#getBean(Class<T>) to return the primary bean (like it is for application contexts). Instead it throws following error:


java.lang.AssertionError: 
Expecting:
 <Started application [AnnotationConfigWebApplicationContext@5c909414 id = 'org.springframework.web.context.support.AnnotationConfigWebApplicationContext@5c909414', applicationName = '', beanDefinitionCount = 76]>
single bean of type:
 <de.codecentric.boot.admin.server.notify.Notifier>
but found:
 <["testNotifierPrimary", "testNotifier2"]>
bug

All 2 comments

Yeah, that is a bit confusing. I'm a little worried that we can't easily change this now without breaking back compatibility. You can do getApplicationContext().getBean(...) as a work-around.

I think we should align with Spring Framework's behaviour and I think we should do it in 2.1. I can just about convince myself that the current behaviour is a bug (due to it not matching Framework's behaviour) and I think that it's acceptable to make a breaking change in a minor release to fix what's arguably a bug particularly when it will only affect test code.

Was this page helpful?
0 / 5 - 0 ratings