Affects versions: 2.5.7+
I believe this is result of moving this line, see details below.
Our Spring Boot application has explicit dependency to spring-boot-starter-webflux
and transitive dependency to spring-boot-starter-web
, but in test/resources/application.properties
we declared:
spring.main.web-application-type=reactive
and we've got test:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) // (1)
class ReactiveOrServletApplicationTests {
@Autowired(required = false)
private org.springframework.boot.web.servlet.error.ErrorAttributes errorAttributesServlet; // (2)
@Autowired(required = false)
private org.springframework.boot.web.reactive.error.ErrorAttributes errorAttributesReactive;
@Test
void contextLoads() {
assertThat(errorAttributesServlet).isNull();
assertThat(errorAttributesReactive).isNotNull();
}
}
Please note:
- (1)
is important to declare webEnvironment
because if effectively creates AbstractBeanFactory.scopes
empty. That's important because of https://github.com/spring-projects/spring-boot/issues/29170
(2)
we try to inject someservlet
component, we expectnull
Running such tests will fail, because Spring will try to define conflicting beans:
- org.springframework.boot.web.servlet.error.DefaultErrorAttributes
(from ErrorMvcAutoConfiguration
)
- org.springframework.boot.web.reactive.error.DefaultErrorAttributes
(from ErrorWebFluxAutoConfiguration
)
Such tests correctly passes on 2.5.6
.
Mentioned change effectively changes what will be assigned here: https://github.com/spring-projects/spring-boot/blob/7c45313bc2e2343ff8dfea342ed1989efb900c88/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java#L332
Before change it was StandardEnvironment
, after change it is ApplicationServletEnvironment
. Difference changes evaluation (from false
to true
) in:
https://github.com/spring-projects/spring-boot/blob/7c45313bc2e2343ff8dfea342ed1989efb900c88/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnWebApplicationCondition.java#L138-L140
And this messes with type of web application:
ErrorMvcAutoConfiguration matched:
- @ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)
- found ConfigurableWebEnvironment (OnWebApplicationCondition)
Comment From: fdimauro
I just ran into this issue. Looks like I may not be able to update my code base to Spring Boot 2.6!
Comment From: philwebb
@mat-mik (or @fdimauro) I'm having some trouble replicating the problem. I've created a sample application at https://github.com/philwebb/scratch-gh-29169, but I can't get the test to fail. Can you try and modify the sample so that it fails in the way that you describe?
Comment From: mat-mik
Hi @philwebb! Thank you for looking at this issue, I submitted Pull Request to your branch - https://github.com/philwebb/scratch-gh-29169/pull/1 I found out that dependency should be spring-boot-starter-web
instead of spring-mvc
, I changed issue description.
Comment From: philwebb
Reopening after review by @mbhave where it was pointed out that by creating another Environment
subclass we're missing the ConfigurationPropertySources
. Worse, we'll likely miss any updates that we make to ApplicationEnvironment
etc. We're going to see if a different fix is possible.