Skip to content

Commit

Permalink
Fix property source ordering in SpringBootTest
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mbhave authored and philwebb committed Nov 24, 2021
1 parent e6b5be9 commit 49e4088
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Expand Up @@ -27,6 +27,7 @@
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;
Expand Down Expand Up @@ -133,14 +134,17 @@ private ConfigurableEnvironment getEnvironment(SpringApplication application, St

private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles,
boolean applicationEnvironment) {
if (ObjectUtils.isEmpty(profiles)) {
return;
}
if (!applicationEnvironment) {
environment.setActiveProfiles(profiles);
}
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);
TestPropertyValues.of(pairs).applyTo(environment, Type.MAP, "active-test-profiles");
}

/**
Expand Down
Expand Up @@ -21,8 +21,10 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.MergedContextConfiguration;
Expand All @@ -38,6 +40,7 @@
*
* @author Stephane Nicoll
* @author Scott Frederick
* @author Madhura Bhave
*/
class SpringBootContextLoaderTests {

Expand Down Expand Up @@ -82,10 +85,9 @@ void environmentPropertiesAnotherSeparatorInValue() {
assertKey(config, "anotherKey", "another=Value");
}

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

@Test
// gh-28776
void testPropertyValuesShouldTakePrecedenceWhenInlinedPropertiesPresent() {
TestContext context = new ExposedTestContextManager(SimpleConfig.class).getExposedTestContext();
StandardEnvironment environment = (StandardEnvironment) context.getApplicationContext().getEnvironment();
TestPropertyValues.of("key=thisValue").applyTo(environment);
assertThat(environment.getProperty("key")).isEqualTo("thisValue");
assertThat(environment.getPropertySources().get("active-test-profiles")).isNull();
}

@Test
void testPropertyValuesShouldTakePrecedenceWhenInlinedPropertiesPresentAndProfilesActive() {
TestContext context = new ExposedTestContextManager(ActiveProfileWithInlinedProperties.class)
.getExposedTestContext();
StandardEnvironment environment = (StandardEnvironment) context.getApplicationContext().getEnvironment();
TestPropertyValues.of("key=thisValue").applyTo(environment);
assertThat(environment.getProperty("key")).isEqualTo("thisValue");
assertThat(environment.getPropertySources().get("active-test-profiles")).isNotNull();
}

private String[] getActiveProfiles(Class<?> testClass) {
TestContext testContext = new ExposedTestContextManager(testClass).getExposedTestContext();
ApplicationContext applicationContext = testContext.getApplicationContext();
Expand Down Expand Up @@ -180,6 +202,13 @@ static class ActiveProfileWithComma {

}

@SpringBootTest({ "key=myValue" })
@ActiveProfiles({ "profile1,2" })
@ContextConfiguration(classes = Config.class)
static class ActiveProfileWithInlinedProperties {

}

@Configuration(proxyBeanMethods = false)
static class Config {

Expand Down

0 comments on commit 49e4088

Please sign in to comment.