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 declarewebEnvironment
because if effectively createsAbstractBeanFactory.scopes
empty. That's important because of Respect WebApplicationType.REACTIVE in tests with a mock web environment #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
(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:
Before change it was StandardEnvironment
, after change it is ApplicationServletEnvironment
. Difference changes evaluation (from false
to true
) in:
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)