-
Notifications
You must be signed in to change notification settings - Fork 41.6k
Description
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 declarewebEnvironmentbecause if effectively createsAbstractBeanFactory.scopesempty. That's important because of Respect WebApplicationType.REACTIVE in tests with a mock web environment #29170 -
(2)we try to inject someservletcomponent, we expectnull
Running such tests will fail, because Spring will try to define conflicting beans:
org.springframework.boot.web.servlet.error.DefaultErrorAttributes(fromErrorMvcAutoConfiguration)org.springframework.boot.web.reactive.error.DefaultErrorAttributes(fromErrorWebFluxAutoConfiguration)
Such tests correctly passes on 2.5.6.
Mentioned change effectively changes what will be assigned here:
Line 332 in 7c45313
| ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments); |
Before change it was StandardEnvironment, after change it is ApplicationServletEnvironment. Difference changes evaluation (from false to true) in:
Lines 138 to 140 in 7c45313
| if (context.getEnvironment() instanceof ConfigurableWebEnvironment) { | |
| return ConditionOutcome.match(message.foundExactly("ConfigurableWebEnvironment")); | |
| } |
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)