Skip to content

Respect WebApplicationType.REACTIVE in tests with a mock web environment #29170

Closed
@mat-mik

Description

@mat-mik

Affects versions: 2.6.2

If you create Spring Boot application with spring-boot-starter-web and spring-boot-starter-webflux depdencencies, but in test/resources/application.properties declare:

spring.main.web-application-type=reactive

and create test:

@SpringBootTest
class ReactiveOrServletApplicationTests {

    @Autowired(required = false)
    private org.springframework.boot.web.servlet.error.ErrorAttributes errorAttributesServlet;

    @Autowired(required = false)
    private org.springframework.boot.web.reactive.error.ErrorAttributes errorAttributesReactive;

    @Test
    void contextLoads() {
        assertThat(errorAttributesServlet).isNull();
        assertThat(errorAttributesReactive).isNotNull();
    }

}

assertions will fail - Spring will create Servlet beans and ignore Reactive beans. Please see report:

   ErrorWebFluxAutoConfiguration:
      Did not match:
         - not a reactive web application (OnWebApplicationCondition)
      Matched:
         - @ConditionalOnClass found required class 'org.springframework.web.reactive.config.WebFluxConfigurer' (OnClassCondition)

   ErrorMvcAutoConfiguration matched:
      - @ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)
      - found 'session' scope (OnWebApplicationCondition)

That's somehow similar to #29169 Here a reason is evaluation:

String[] scopes = context.getBeanFactory().getRegisteredScopeNames();
if (ObjectUtils.containsElement(scopes, "session")) {
return ConditionOutcome.match(message.foundExactly("'session' scope"));
}
because GenericWebApplicationContext is used as ConfigurableApplicationContext

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions