Skip to content

Commit 6d6bbfb

Browse files
committed
Merge pull request #19556 from scottfrederick
* pr/19556: Add profiles directly to the application environment for tests Closes gh-19556
2 parents d46406f + 487b9cb commit 6d6bbfb

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
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;
3231
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
3332
import org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer;
3433
import org.springframework.context.ApplicationContext;
@@ -73,6 +72,7 @@
7372
* @author Andy Wilkinson
7473
* @author Stephane Nicoll
7574
* @author Madhura Bhave
75+
* @author Scott Frederick
7676
* @since 1.4.0
7777
* @see SpringBootTest
7878
*/
@@ -92,7 +92,7 @@ public ApplicationContext loadContext(MergedContextConfiguration config) throws
9292
application.getSources().addAll(Arrays.asList(configLocations));
9393
ConfigurableEnvironment environment = getEnvironment();
9494
if (!ObjectUtils.isEmpty(config.getActiveProfiles())) {
95-
setActiveProfiles(environment, config.getActiveProfiles());
95+
environment.setActiveProfiles(config.getActiveProfiles());
9696
}
9797
ResourceLoader resourceLoader = (application.getResourceLoader() != null) ? application.getResourceLoader()
9898
: new DefaultResourceLoader(getClass().getClassLoader());
@@ -138,11 +138,6 @@ protected ConfigurableEnvironment getEnvironment() {
138138
return new StandardEnvironment();
139139
}
140140

141-
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) {
142-
TestPropertyValues.of("spring.profiles.active=" + StringUtils.arrayToCommaDelimitedString(profiles))
143-
.applyTo(environment);
144-
}
145-
146141
protected String[] getInlinedProperties(MergedContextConfiguration config) {
147142
ArrayList<String> properties = new ArrayList<>();
148143
// 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: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
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;
2527
import org.springframework.test.context.ContextConfiguration;
2628
import org.springframework.test.context.MergedContextConfiguration;
2729
import org.springframework.test.context.TestContext;
@@ -35,6 +37,7 @@
3537
* Tests for {@link SpringBootContextLoader}
3638
*
3739
* @author Stephane Nicoll
40+
* @author Scott Frederick
3841
*/
3942
public class SpringBootContextLoaderTests {
4043

@@ -88,13 +91,40 @@ public void environmentPropertiesNewLineInValue() {
8891
assertKey(config, "variables", "foo=FOO\n bar=BAR");
8992
}
9093

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+
91112
private Map<String, Object> getEnvironmentProperties(Class<?> testClass) {
92-
TestContext context = new ExposedTestContextManager(testClass).getExposedTestContext();
113+
TestContext context = getTestContext(testClass);
93114
MergedContextConfiguration config = (MergedContextConfiguration) ReflectionTestUtils.getField(context,
94115
"mergedContextConfiguration");
95116
return TestPropertySourceUtils.convertInlinedPropertiesToMap(config.getPropertySourceProperties());
96117
}
97118

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+
98128
private void assertKey(Map<String, Object> actual, String key, Object value) {
99129
assertThat(actual.containsKey(key)).as("Key '" + key + "' not found").isTrue();
100130
assertThat(actual.get(key)).isEqualTo(value);
@@ -142,6 +172,20 @@ static class NewLineInValue {
142172

143173
}
144174

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+
145189
@Configuration
146190
static class Config {
147191

0 commit comments

Comments
 (0)