Skip to content

Commit c85f19b

Browse files
committed
Revert "Add profiles directly to the application environment for tests"
This reverts commit 487b9cb. Fixes gh-19788
1 parent 11efe17 commit c85f19b

File tree

2 files changed

+8
-45
lines changed

2 files changed

+8
-45
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
2929
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
3030
import org.springframework.boot.test.mock.web.SpringBootMockServletContext;
31+
import org.springframework.boot.test.util.TestPropertyValues;
3132
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
3233
import org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer;
3334
import org.springframework.context.ApplicationContext;
@@ -92,7 +93,7 @@ public ApplicationContext loadContext(MergedContextConfiguration config) throws
9293
application.getSources().addAll(Arrays.asList(configLocations));
9394
ConfigurableEnvironment environment = getEnvironment();
9495
if (!ObjectUtils.isEmpty(config.getActiveProfiles())) {
95-
environment.setActiveProfiles(config.getActiveProfiles());
96+
setActiveProfiles(environment, config.getActiveProfiles());
9697
}
9798
ResourceLoader resourceLoader = (application.getResourceLoader() != null) ? application.getResourceLoader()
9899
: new DefaultResourceLoader(getClass().getClassLoader());
@@ -138,6 +139,11 @@ protected ConfigurableEnvironment getEnvironment() {
138139
return new StandardEnvironment();
139140
}
140141

142+
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) {
143+
TestPropertyValues.of("spring.profiles.active=" + StringUtils.arrayToCommaDelimitedString(profiles))
144+
.applyTo(environment);
145+
}
146+
141147
protected String[] getInlinedProperties(MergedContextConfiguration config) {
142148
ArrayList<String> properties = new ArrayList<>();
143149
// JMX bean names will clash if the same bean is used in multiple contexts

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import org.junit.Test;
2323

2424
import org.springframework.context.annotation.Configuration;
25-
import org.springframework.core.env.Environment;
26-
import org.springframework.test.context.ActiveProfiles;
2725
import org.springframework.test.context.ContextConfiguration;
2826
import org.springframework.test.context.MergedContextConfiguration;
2927
import org.springframework.test.context.TestContext;
@@ -91,40 +89,13 @@ public void environmentPropertiesNewLineInValue() {
9189
assertKey(config, "variables", "foo=FOO\n bar=BAR");
9290
}
9391

94-
@Test
95-
public void noActiveProfiles() {
96-
Environment environment = getApplicationEnvironment(SimpleConfig.class);
97-
assertThat(environment.getActiveProfiles()).isEmpty();
98-
}
99-
100-
@Test
101-
public void multipleActiveProfiles() {
102-
Environment environment = getApplicationEnvironment(MultipleActiveProfiles.class);
103-
assertThat(environment.getActiveProfiles()).containsExactly("profile1", "profile2");
104-
}
105-
106-
@Test
107-
public void activeProfileWithComma() {
108-
Environment environment = getApplicationEnvironment(ActiveProfileWithComma.class);
109-
assertThat(environment.getActiveProfiles()).containsExactly("profile1,2");
110-
}
111-
11292
private Map<String, Object> getEnvironmentProperties(Class<?> testClass) {
113-
TestContext context = getTestContext(testClass);
93+
TestContext context = new ExposedTestContextManager(testClass).getExposedTestContext();
11494
MergedContextConfiguration config = (MergedContextConfiguration) ReflectionTestUtils.getField(context,
11595
"mergedContextConfiguration");
11696
return TestPropertySourceUtils.convertInlinedPropertiesToMap(config.getPropertySourceProperties());
11797
}
11898

119-
private Environment getApplicationEnvironment(Class<?> testClass) {
120-
TestContext context = getTestContext(testClass);
121-
return context.getApplicationContext().getEnvironment();
122-
}
123-
124-
private TestContext getTestContext(Class<?> testClass) {
125-
return new ExposedTestContextManager(testClass).getExposedTestContext();
126-
}
127-
12899
private void assertKey(Map<String, Object> actual, String key, Object value) {
129100
assertThat(actual.containsKey(key)).as("Key '" + key + "' not found").isTrue();
130101
assertThat(actual.get(key)).isEqualTo(value);
@@ -172,20 +143,6 @@ static class NewLineInValue {
172143

173144
}
174145

175-
@SpringBootTest
176-
@ActiveProfiles({ "profile1", "profile2" })
177-
@ContextConfiguration(classes = Config.class)
178-
static class MultipleActiveProfiles {
179-
180-
}
181-
182-
@SpringBootTest
183-
@ActiveProfiles({ "profile1,2" })
184-
@ContextConfiguration(classes = Config.class)
185-
static class ActiveProfileWithComma {
186-
187-
}
188-
189146
@Configuration
190147
static class Config {
191148

0 commit comments

Comments
 (0)