|
|
|
@ -91,16 +91,6 @@ public class SpringBootContextLoader extends AbstractContextLoader {
|
|
|
|
|
application.setMainApplicationClass(config.getTestClass());
|
|
|
|
|
application.addPrimarySources(Arrays.asList(configClasses));
|
|
|
|
|
application.getSources().addAll(Arrays.asList(configLocations));
|
|
|
|
|
ConfigurableEnvironment environment = getEnvironment();
|
|
|
|
|
if (!ObjectUtils.isEmpty(config.getActiveProfiles())) {
|
|
|
|
|
setActiveProfiles(environment, config.getActiveProfiles());
|
|
|
|
|
}
|
|
|
|
|
ResourceLoader resourceLoader = (application.getResourceLoader() != null) ? application.getResourceLoader()
|
|
|
|
|
: new DefaultResourceLoader(null);
|
|
|
|
|
TestPropertySourceUtils.addPropertiesFilesToEnvironment(environment, resourceLoader,
|
|
|
|
|
config.getPropertySourceLocations());
|
|
|
|
|
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, getInlinedProperties(config));
|
|
|
|
|
application.setEnvironment(environment);
|
|
|
|
|
List<ApplicationContextInitializer<?>> initializers = getInitializers(config, application);
|
|
|
|
|
if (config instanceof WebMergedContextConfiguration) {
|
|
|
|
|
application.setWebApplicationType(WebApplicationType.SERVLET);
|
|
|
|
@ -119,10 +109,40 @@ public class SpringBootContextLoader extends AbstractContextLoader {
|
|
|
|
|
application.setWebApplicationType(WebApplicationType.NONE);
|
|
|
|
|
}
|
|
|
|
|
application.setInitializers(initializers);
|
|
|
|
|
ConfigurableEnvironment environment = getEnvironment(application, config.getActiveProfiles());
|
|
|
|
|
ResourceLoader resourceLoader = (application.getResourceLoader() != null) ? application.getResourceLoader()
|
|
|
|
|
: new DefaultResourceLoader(null);
|
|
|
|
|
TestPropertySourceUtils.addPropertiesFilesToEnvironment(environment, resourceLoader,
|
|
|
|
|
config.getPropertySourceLocations());
|
|
|
|
|
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, getInlinedProperties(config));
|
|
|
|
|
application.setEnvironment(environment);
|
|
|
|
|
String[] args = SpringBootTestArgs.get(config.getContextCustomizers());
|
|
|
|
|
return application.run(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ConfigurableEnvironment getEnvironment(SpringApplication application, String[] activeProfiles) {
|
|
|
|
|
ConfigurableEnvironment environment = getEnvironment();
|
|
|
|
|
boolean applicationEnvironment = false;
|
|
|
|
|
if (environment.getClass() == StandardEnvironment.class) {
|
|
|
|
|
environment = application.convertEnvironment(environment);
|
|
|
|
|
applicationEnvironment = true;
|
|
|
|
|
}
|
|
|
|
|
setActiveProfiles(environment, activeProfiles, applicationEnvironment);
|
|
|
|
|
return environment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles,
|
|
|
|
|
boolean applicationEnvironment) {
|
|
|
|
|
if (!applicationEnvironment) {
|
|
|
|
|
environment.setActiveProfiles(profiles);
|
|
|
|
|
}
|
|
|
|
|
String[] pairs = new String[profiles.length];
|
|
|
|
|
for (int i = 0; i < profiles.length; i++) {
|
|
|
|
|
pairs[i] = "spring.profiles.active[" + i + "]=" + profiles[i];
|
|
|
|
|
}
|
|
|
|
|
TestPropertyValues.of(pairs).applyTo(environment);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Builds new {@link org.springframework.boot.SpringApplication} instance. You can
|
|
|
|
|
* override this method to add custom behavior
|
|
|
|
@ -141,16 +161,6 @@ public class SpringBootContextLoader extends AbstractContextLoader {
|
|
|
|
|
return new StandardEnvironment();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) {
|
|
|
|
|
environment.setActiveProfiles(profiles);
|
|
|
|
|
// Also add as properties to override any application.properties
|
|
|
|
|
String[] pairs = new String[profiles.length];
|
|
|
|
|
for (int i = 0; i < profiles.length; i++) {
|
|
|
|
|
pairs[i] = "spring.profiles.active[" + i + "]=" + profiles[i];
|
|
|
|
|
}
|
|
|
|
|
TestPropertyValues.of(pairs).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
|
|
|
|
|