Add profiles directly to the application environment for tests

Prior to this commit, active profiles were being added to the Spring Boot
application environment by setting the `spring.profiles.active` property.
This could result in profiles getting parsed differently than other uses of `@ActiveProfiles`.
Setting the profiles directly in the `Environment` prevents this parsing.

See gh-19556
This commit is contained in:
Scott Frederick
2020-01-07 09:53:32 -06:00
committed by Madhura Bhave
parent d46406fcb9
commit 487b9cbf96
2 changed files with 47 additions and 8 deletions

View File

@@ -22,6 +22,8 @@ import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContext;
@@ -35,6 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link SpringBootContextLoader}
*
* @author Stephane Nicoll
* @author Scott Frederick
*/
public class SpringBootContextLoaderTests {
@@ -88,13 +91,40 @@ public class SpringBootContextLoaderTests {
assertKey(config, "variables", "foo=FOO\n bar=BAR");
}
@Test
public void noActiveProfiles() {
Environment environment = getApplicationEnvironment(SimpleConfig.class);
assertThat(environment.getActiveProfiles()).isEmpty();
}
@Test
public void multipleActiveProfiles() {
Environment environment = getApplicationEnvironment(MultipleActiveProfiles.class);
assertThat(environment.getActiveProfiles()).containsExactly("profile1", "profile2");
}
@Test
public void activeProfileWithComma() {
Environment environment = getApplicationEnvironment(ActiveProfileWithComma.class);
assertThat(environment.getActiveProfiles()).containsExactly("profile1,2");
}
private Map<String, Object> getEnvironmentProperties(Class<?> testClass) {
TestContext context = new ExposedTestContextManager(testClass).getExposedTestContext();
TestContext context = getTestContext(testClass);
MergedContextConfiguration config = (MergedContextConfiguration) ReflectionTestUtils.getField(context,
"mergedContextConfiguration");
return TestPropertySourceUtils.convertInlinedPropertiesToMap(config.getPropertySourceProperties());
}
private Environment getApplicationEnvironment(Class<?> testClass) {
TestContext context = getTestContext(testClass);
return context.getApplicationContext().getEnvironment();
}
private TestContext getTestContext(Class<?> testClass) {
return new ExposedTestContextManager(testClass).getExposedTestContext();
}
private void assertKey(Map<String, Object> actual, String key, Object value) {
assertThat(actual.containsKey(key)).as("Key '" + key + "' not found").isTrue();
assertThat(actual.get(key)).isEqualTo(value);
@@ -142,6 +172,20 @@ public class SpringBootContextLoaderTests {
}
@SpringBootTest
@ActiveProfiles({ "profile1", "profile2" })
@ContextConfiguration(classes = Config.class)
static class MultipleActiveProfiles {
}
@SpringBootTest
@ActiveProfiles({ "profile1,2" })
@ContextConfiguration(classes = Config.class)
static class ActiveProfileWithComma {
}
@Configuration
static class Config {