diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java index e90d067a5f6..91630ea97b0 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java @@ -153,8 +153,11 @@ public class SpringBootContextLoader extends AbstractContextLoader { } private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) { - TestPropertyValues.of("spring.profiles.active=" + StringUtils.arrayToCommaDelimitedString(profiles)) - .applyTo(environment); + String[] pairs = new String[profiles.length]; + for (int i = 0; i < profiles.length; i++) { + pairs[i] = "spring.profiles.active[" + i + "]=" + profiles[i]; + } + TestPropertyValues.of(pairs).applyTo(environment); } protected String[] getInlinedProperties(MergedContextConfiguration config) { diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java index 0ca15147de5..e34568306f3 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java @@ -21,7 +21,9 @@ import java.util.Map; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.MergedContextConfiguration; import org.springframework.test.context.TestContext; @@ -41,41 +43,41 @@ class SpringBootContextLoaderTests { @Test void environmentPropertiesSimple() { - Map config = getEnvironmentProperties(SimpleConfig.class); + Map config = getMergedContextConfigurationProperties(SimpleConfig.class); assertKey(config, "key", "myValue"); assertKey(config, "anotherKey", "anotherValue"); } @Test void environmentPropertiesSimpleNonAlias() { - Map config = getEnvironmentProperties(SimpleConfigNonAlias.class); + Map config = getMergedContextConfigurationProperties(SimpleConfigNonAlias.class); assertKey(config, "key", "myValue"); assertKey(config, "anotherKey", "anotherValue"); } @Test void environmentPropertiesOverrideDefaults() { - Map config = getEnvironmentProperties(OverrideConfig.class); + Map config = getMergedContextConfigurationProperties(OverrideConfig.class); assertKey(config, "server.port", "2345"); } @Test void environmentPropertiesAppend() { - Map config = getEnvironmentProperties(AppendConfig.class); + Map config = getMergedContextConfigurationProperties(AppendConfig.class); assertKey(config, "key", "myValue"); assertKey(config, "otherKey", "otherValue"); } @Test void environmentPropertiesSeparatorInValue() { - Map config = getEnvironmentProperties(SameSeparatorInValue.class); + Map config = getMergedContextConfigurationProperties(SameSeparatorInValue.class); assertKey(config, "key", "my=Value"); assertKey(config, "anotherKey", "another:Value"); } @Test void environmentPropertiesAnotherSeparatorInValue() { - Map config = getEnvironmentProperties(AnotherSeparatorInValue.class); + Map config = getMergedContextConfigurationProperties(AnotherSeparatorInValue.class); assertKey(config, "key", "my:Value"); assertKey(config, "anotherKey", "another=Value"); } @@ -84,12 +86,33 @@ class SpringBootContextLoaderTests { @Disabled void environmentPropertiesNewLineInValue() { // gh-4384 - Map config = getEnvironmentProperties(NewLineInValue.class); + Map config = getMergedContextConfigurationProperties(NewLineInValue.class); assertKey(config, "key", "myValue"); assertKey(config, "variables", "foo=FOO\n bar=BAR"); } - private Map getEnvironmentProperties(Class testClass) { + @Test + void noActiveProfiles() { + assertThat(getActiveProfiles(SimpleConfig.class)).isEmpty(); + } + + @Test + void multipleActiveProfiles() { + assertThat(getActiveProfiles(MultipleActiveProfiles.class)).containsExactly("profile1", "profile2"); + } + + @Test + void activeProfileWithComma() { + assertThat(getActiveProfiles(ActiveProfileWithComma.class)).containsExactly("profile1,2"); + } + + private String[] getActiveProfiles(Class testClass) { + TestContext testContext = new ExposedTestContextManager(testClass).getExposedTestContext(); + ApplicationContext applicationContext = testContext.getApplicationContext(); + return applicationContext.getEnvironment().getActiveProfiles(); + } + + private Map getMergedContextConfigurationProperties(Class testClass) { TestContext context = new ExposedTestContextManager(testClass).getExposedTestContext(); MergedContextConfiguration config = (MergedContextConfiguration) ReflectionTestUtils.getField(context, "mergedContextConfiguration"); @@ -143,6 +166,20 @@ class SpringBootContextLoaderTests { } + @SpringBootTest + @ActiveProfiles({ "profile1", "profile2" }) + @ContextConfiguration(classes = Config.class) + static class MultipleActiveProfiles { + + } + + @SpringBootTest + @ActiveProfiles({ "profile1,2" }) + @ContextConfiguration(classes = Config.class) + static class ActiveProfileWithComma { + + } + @Configuration(proxyBeanMethods = false) static class Config {