Skip to content

@SpringBootTest does not use spring.main.web-application-type properties declared in test resource files #29169

Closed
@mat-mik

Description

@mat-mik

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:

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:

ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);

Before change it was StandardEnvironment, after change it is ApplicationServletEnvironment. Difference changes evaluation (from false to true) in:

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)

Metadata

Metadata

Assignees

Labels

type: regressionA regression from a previous release

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions