Skip to content

Commit 27eb992

Browse files
mbhavephilwebb
authored andcommitted
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. Closes gh-28804
1 parent 12244a8 commit 27eb992

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2828
import org.springframework.boot.test.mock.web.SpringBootMockServletContext;
2929
import org.springframework.boot.test.util.TestPropertyValues;
30+
import org.springframework.boot.test.util.TestPropertyValues.Type;
3031
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
3132
import org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer;
3233
import org.springframework.context.ApplicationContext;
@@ -133,14 +134,17 @@ private ConfigurableEnvironment getEnvironment(SpringApplication application, St
133134

134135
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles,
135136
boolean applicationEnvironment) {
137+
if (ObjectUtils.isEmpty(profiles)) {
138+
return;
139+
}
136140
if (!applicationEnvironment) {
137141
environment.setActiveProfiles(profiles);
138142
}
139143
String[] pairs = new String[profiles.length];
140144
for (int i = 0; i < profiles.length; i++) {
141145
pairs[i] = "spring.profiles.active[" + i + "]=" + profiles[i];
142146
}
143-
TestPropertyValues.of(pairs).applyTo(environment);
147+
TestPropertyValues.of(pairs).applyTo(environment, Type.MAP, "active-test-profiles");
144148
}
145149

146150
/**

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import org.junit.jupiter.api.Disabled;
2222
import org.junit.jupiter.api.Test;
2323

24+
import org.springframework.boot.test.util.TestPropertyValues;
2425
import org.springframework.context.ApplicationContext;
2526
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.core.env.StandardEnvironment;
2628
import org.springframework.test.context.ActiveProfiles;
2729
import org.springframework.test.context.ContextConfiguration;
2830
import org.springframework.test.context.MergedContextConfiguration;
@@ -38,6 +40,7 @@
3840
*
3941
* @author Stephane Nicoll
4042
* @author Scott Frederick
43+
* @author Madhura Bhave
4144
*/
4245
class SpringBootContextLoaderTests {
4346

@@ -82,10 +85,9 @@ void environmentPropertiesAnotherSeparatorInValue() {
8285
assertKey(config, "anotherKey", "another=Value");
8386
}
8487

85-
@Test
88+
@Test // gh-4384
8689
@Disabled
8790
void environmentPropertiesNewLineInValue() {
88-
// gh-4384
8991
Map<String, Object> config = getMergedContextConfigurationProperties(NewLineInValue.class);
9092
assertKey(config, "key", "myValue");
9193
assertKey(config, "variables", "foo=FOO\n bar=BAR");
@@ -106,6 +108,26 @@ void activeProfileWithComma() {
106108
assertThat(getActiveProfiles(ActiveProfileWithComma.class)).containsExactly("profile1,2");
107109
}
108110

111+
@Test
112+
// gh-28776
113+
void testPropertyValuesShouldTakePrecedenceWhenInlinedPropertiesPresent() {
114+
TestContext context = new ExposedTestContextManager(SimpleConfig.class).getExposedTestContext();
115+
StandardEnvironment environment = (StandardEnvironment) context.getApplicationContext().getEnvironment();
116+
TestPropertyValues.of("key=thisValue").applyTo(environment);
117+
assertThat(environment.getProperty("key")).isEqualTo("thisValue");
118+
assertThat(environment.getPropertySources().get("active-test-profiles")).isNull();
119+
}
120+
121+
@Test
122+
void testPropertyValuesShouldTakePrecedenceWhenInlinedPropertiesPresentAndProfilesActive() {
123+
TestContext context = new ExposedTestContextManager(ActiveProfileWithInlinedProperties.class)
124+
.getExposedTestContext();
125+
StandardEnvironment environment = (StandardEnvironment) context.getApplicationContext().getEnvironment();
126+
TestPropertyValues.of("key=thisValue").applyTo(environment);
127+
assertThat(environment.getProperty("key")).isEqualTo("thisValue");
128+
assertThat(environment.getPropertySources().get("active-test-profiles")).isNotNull();
129+
}
130+
109131
private String[] getActiveProfiles(Class<?> testClass) {
110132
TestContext testContext = new ExposedTestContextManager(testClass).getExposedTestContext();
111133
ApplicationContext applicationContext = testContext.getApplicationContext();
@@ -180,6 +202,13 @@ static class ActiveProfileWithComma {
180202

181203
}
182204

205+
@SpringBootTest({ "key=myValue" })
206+
@ActiveProfiles({ "profile1,2" })
207+
@ContextConfiguration(classes = Config.class)
208+
static class ActiveProfileWithInlinedProperties {
209+
210+
}
211+
183212
@Configuration(proxyBeanMethods = false)
184213
static class Config {
185214

0 commit comments

Comments
 (0)