diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java index 4990c8f14cb8..0485ddfab6f8 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java @@ -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; @@ -133,6 +134,9 @@ 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); } @@ -140,7 +144,7 @@ private void setActiveProfiles(ConfigurableEnvironment environment, String[] pro 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"); } /** diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java index e34568306f32..f06016c6d6b9 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java @@ -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; @@ -38,6 +40,7 @@ * * @author Stephane Nicoll * @author Scott Frederick + * @author Madhura Bhave */ class SpringBootContextLoaderTests { @@ -82,10 +85,9 @@ void environmentPropertiesAnotherSeparatorInValue() { assertKey(config, "anotherKey", "another=Value"); } - @Test + @Test // gh-4384 @Disabled void environmentPropertiesNewLineInValue() { - // gh-4384 Map config = getMergedContextConfigurationProperties(NewLineInValue.class); assertKey(config, "key", "myValue"); assertKey(config, "variables", "foo=FOO\n bar=BAR"); @@ -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(); @@ -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 {