Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mat-mik opened this issue Dec 24, 2021 · 4 comments
Assignees
Labels
type: regression A regression from a previous release
Milestone

Comments

@mat-mik
Copy link

mat-mik commented Dec 24, 2021

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)
@fdimauro
Copy link

fdimauro commented Dec 30, 2021

I just ran into this issue. Looks like I may not be able to update my code base to Spring Boot 2.6!

@philwebb
Copy link
Member

@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?

@philwebb philwebb added the status: waiting-for-feedback We need additional information before we can continue label Jan 10, 2022
@mat-mik
Copy link
Author

mat-mik commented Jan 11, 2022

Hi @philwebb! Thank you for looking at this issue, I submitted Pull Request to your branch - philwebb/scratch-gh-29169#1 I found out that dependency should be spring-boot-starter-web instead of spring-mvc, I changed issue description.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jan 11, 2022
@philwebb philwebb added type: regression A regression from a previous release and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Jan 11, 2022
@philwebb philwebb added this to the 2.5.x milestone Jan 11, 2022
@philwebb philwebb self-assigned this Jan 11, 2022
@philwebb philwebb changed the title Spring Boot does not respect WebApplicationType.REACTIVE in tests anymore (when webEnvironment specified) @SpringBootTest does not use spring.main.web-application-type properties declared in test resource files Jan 12, 2022
@philwebb philwebb modified the milestones: 2.5.x, 2.5.9 Jan 12, 2022
@philwebb philwebb reopened this Jan 13, 2022
@philwebb
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

4 participants