Fix property source ordering in SpringBootTest

Update `SpringBootContextLoader` so that the active profiles
property source has a unique name. Prior to this commit, the
default name 'test' was used which could cause ordering issues
if other `@PropertySource` values were added to it later.

Fixes gh-28776
This commit is contained in:
Madhura Bhave
2021-11-23 12:53:07 -08:00
committed by Phillip Webb
parent e6b5be900a
commit 49e408828c
2 changed files with 36 additions and 3 deletions

View File

@@ -27,6 +27,7 @@ import org.springframework.boot.WebApplicationType;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.web.SpringBootMockServletContext;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.test.util.TestPropertyValues.Type;
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
import org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer;
import org.springframework.context.ApplicationContext;
@@ -133,6 +134,9 @@ public class SpringBootContextLoader extends AbstractContextLoader {
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles,
boolean applicationEnvironment) {
if (ObjectUtils.isEmpty(profiles)) {
return;
}
if (!applicationEnvironment) {
environment.setActiveProfiles(profiles);
}
@@ -140,7 +144,7 @@ public class SpringBootContextLoader extends AbstractContextLoader {
for (int i = 0; i < profiles.length; i++) {
pairs[i] = "spring.profiles.active[" + i + "]=" + profiles[i];
}
TestPropertyValues.of(pairs).applyTo(environment);
TestPropertyValues.of(pairs).applyTo(environment, Type.MAP, "active-test-profiles");
}
/**