|  | 
| 22 | 22 | import org.junit.Test; | 
| 23 | 23 | 
 | 
| 24 | 24 | import org.springframework.context.annotation.Configuration; | 
|  | 25 | +import org.springframework.core.env.Environment; | 
|  | 26 | +import org.springframework.test.context.ActiveProfiles; | 
| 25 | 27 | import org.springframework.test.context.ContextConfiguration; | 
| 26 | 28 | import org.springframework.test.context.MergedContextConfiguration; | 
| 27 | 29 | import org.springframework.test.context.TestContext; | 
|  | 
| 35 | 37 |  * Tests for {@link SpringBootContextLoader} | 
| 36 | 38 |  * | 
| 37 | 39 |  * @author Stephane Nicoll | 
|  | 40 | + * @author Scott Frederick | 
| 38 | 41 |  */ | 
| 39 | 42 | public class SpringBootContextLoaderTests { | 
| 40 | 43 | 
 | 
| @@ -88,13 +91,40 @@ public void environmentPropertiesNewLineInValue() { | 
| 88 | 91 | 		assertKey(config, "variables", "foo=FOO\n bar=BAR"); | 
| 89 | 92 | 	} | 
| 90 | 93 | 
 | 
|  | 94 | +	@Test | 
|  | 95 | +	public void noActiveProfiles() { | 
|  | 96 | +		Environment environment = getApplicationEnvironment(SimpleConfig.class); | 
|  | 97 | +		assertThat(environment.getActiveProfiles()).isEmpty(); | 
|  | 98 | +	} | 
|  | 99 | + | 
|  | 100 | +	@Test | 
|  | 101 | +	public void multipleActiveProfiles() { | 
|  | 102 | +		Environment environment = getApplicationEnvironment(MultipleActiveProfiles.class); | 
|  | 103 | +		assertThat(environment.getActiveProfiles()).containsExactly("profile1", "profile2"); | 
|  | 104 | +	} | 
|  | 105 | + | 
|  | 106 | +	@Test | 
|  | 107 | +	public void activeProfileWithComma() { | 
|  | 108 | +		Environment environment = getApplicationEnvironment(ActiveProfileWithComma.class); | 
|  | 109 | +		assertThat(environment.getActiveProfiles()).containsExactly("profile1,2"); | 
|  | 110 | +	} | 
|  | 111 | + | 
| 91 | 112 | 	private Map<String, Object> getEnvironmentProperties(Class<?> testClass) { | 
| 92 |  | -		TestContext context = new ExposedTestContextManager(testClass).getExposedTestContext(); | 
|  | 113 | +		TestContext context = getTestContext(testClass); | 
| 93 | 114 | 		MergedContextConfiguration config = (MergedContextConfiguration) ReflectionTestUtils.getField(context, | 
| 94 | 115 | 				"mergedContextConfiguration"); | 
| 95 | 116 | 		return TestPropertySourceUtils.convertInlinedPropertiesToMap(config.getPropertySourceProperties()); | 
| 96 | 117 | 	} | 
| 97 | 118 | 
 | 
|  | 119 | +	private Environment getApplicationEnvironment(Class<?> testClass) { | 
|  | 120 | +		TestContext context = getTestContext(testClass); | 
|  | 121 | +		return context.getApplicationContext().getEnvironment(); | 
|  | 122 | +	} | 
|  | 123 | + | 
|  | 124 | +	private TestContext getTestContext(Class<?> testClass) { | 
|  | 125 | +		return new ExposedTestContextManager(testClass).getExposedTestContext(); | 
|  | 126 | +	} | 
|  | 127 | + | 
| 98 | 128 | 	private void assertKey(Map<String, Object> actual, String key, Object value) { | 
| 99 | 129 | 		assertThat(actual.containsKey(key)).as("Key '" + key + "' not found").isTrue(); | 
| 100 | 130 | 		assertThat(actual.get(key)).isEqualTo(value); | 
| @@ -142,6 +172,20 @@ static class NewLineInValue { | 
| 142 | 172 | 
 | 
| 143 | 173 | 	} | 
| 144 | 174 | 
 | 
|  | 175 | +	@SpringBootTest | 
|  | 176 | +	@ActiveProfiles({ "profile1", "profile2" }) | 
|  | 177 | +	@ContextConfiguration(classes = Config.class) | 
|  | 178 | +	static class MultipleActiveProfiles { | 
|  | 179 | + | 
|  | 180 | +	} | 
|  | 181 | + | 
|  | 182 | +	@SpringBootTest | 
|  | 183 | +	@ActiveProfiles({ "profile1,2" }) | 
|  | 184 | +	@ContextConfiguration(classes = Config.class) | 
|  | 185 | +	static class ActiveProfileWithComma { | 
|  | 186 | + | 
|  | 187 | +	} | 
|  | 188 | + | 
| 145 | 189 | 	@Configuration | 
| 146 | 190 | 	static class Config { | 
| 147 | 191 | 
 | 
|  | 
0 commit comments