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

@@ -28,7 +28,6 @@ import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import org.springframework.boot.test.mock.web.SpringBootMockServletContext;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
import org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer;
import org.springframework.context.ApplicationContext;
@@ -73,6 +72,7 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Madhura Bhave
* @author Scott Frederick
* @since 1.4.0
* @see SpringBootTest
*/
@@ -92,7 +92,7 @@ public class SpringBootContextLoader extends AbstractContextLoader {
application.getSources().addAll(Arrays.asList(configLocations));
ConfigurableEnvironment environment = getEnvironment();
if (!ObjectUtils.isEmpty(config.getActiveProfiles())) {
setActiveProfiles(environment, config.getActiveProfiles());
environment.setActiveProfiles(config.getActiveProfiles());
}
ResourceLoader resourceLoader = (application.getResourceLoader() != null) ? application.getResourceLoader()
: new DefaultResourceLoader(getClass().getClassLoader());
@@ -138,11 +138,6 @@ public class SpringBootContextLoader extends AbstractContextLoader {
return new StandardEnvironment();
}
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) {
TestPropertyValues.of("spring.profiles.active=" + StringUtils.arrayToCommaDelimitedString(profiles))
.applyTo(environment);
}
protected String[] getInlinedProperties(MergedContextConfiguration config) {
ArrayList<String> properties = new ArrayList<>();
// JMX bean names will clash if the same bean is used in multiple contexts