Convert environment used by SpringBootTestContextLoader

This commit aligns `SpringBootTest`s to also use `ApplicationEnvironment`
instead of `StandardEnvironment`. This prevents the side-effect of active
profiles from `@ActiveProfiles` from being added to the environment when
doGetActiveProfiles is called. In this case, calling `addActiveProfiles()`
in the environment post processor would result in `@ActiveProfiles` being
added to the environment first, resulting in the wrong order.

The additional call to `setActiveProfiles()` is also not necessary when using
ApplicationEnvironment because that call was put in place to prevent the side-effect
which `ApplicationEnvironment` does not have.

Fixes gh-28530
This commit is contained in:
Madhura Bhave
2021-11-10 16:09:19 -08:00
parent 7fbb9d471b
commit 64270eca51
6 changed files with 197 additions and 22 deletions

View File

@@ -377,13 +377,24 @@ public class SpringApplication {
"Environment prefix cannot be set via properties.");
bindToSpringApplication(environment);
if (!this.isCustomEnvironment) {
environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
deduceEnvironmentClass());
environment = convertEnvironment(environment);
}
ConfigurationPropertySources.attach(environment);
return environment;
}
/**
* Convert the given {@link ConfigurableEnvironment environment} to an application
* environment that doesn't attempt to resolve profile properties directly.
* @param environment the environment to convert
* @return the converted environment
* @since 2.5.7
*/
public StandardEnvironment convertEnvironment(ConfigurableEnvironment environment) {
return new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
deduceEnvironmentClass());
}
private Class<? extends StandardEnvironment> deduceEnvironmentClass() {
switch (this.webApplicationType) {
case SERVLET: