mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
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
This commit is contained in:
parent
12244a8edd
commit
27eb992252
@ -27,6 +27,7 @@ import org.springframework.boot.WebApplicationType;
|
||||
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;
|
||||
@ -133,6 +134,9 @@ public class SpringBootContextLoader extends AbstractContextLoader {
|
||||
|
||||
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles,
|
||||
boolean applicationEnvironment) {
|
||||
if (ObjectUtils.isEmpty(profiles)) {
|
||||
return;
|
||||
}
|
||||
if (!applicationEnvironment) {
|
||||
environment.setActiveProfiles(profiles);
|
||||
}
|
||||
@ -140,7 +144,7 @@ public class SpringBootContextLoader extends AbstractContextLoader {
|
||||
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");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -21,8 +21,10 @@ import java.util.Map;
|
||||
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;
|
||||
@ -38,6 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Scott Frederick
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
class SpringBootContextLoaderTests {
|
||||
|
||||
@ -82,10 +85,9 @@ class SpringBootContextLoaderTests {
|
||||
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");
|
||||
@ -106,6 +108,26 @@ class SpringBootContextLoaderTests {
|
||||
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();
|
||||
@ -180,6 +202,13 @@ class SpringBootContextLoaderTests {
|
||||
|
||||
}
|
||||
|
||||
@SpringBootTest({ "key=myValue" })
|
||||
@ActiveProfiles({ "profile1,2" })
|
||||
@ContextConfiguration(classes = Config.class)
|
||||
static class ActiveProfileWithInlinedProperties {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class Config {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user