use of org.springframework.core.env.ConfigurableEnvironment in project spring-boot by spring-projects.
the class SpringBootContextLoader method loadContext.
@Override
public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception {
SpringApplication application = getSpringApplication();
application.setMainApplicationClass(config.getTestClass());
application.setSources(getSources(config));
ConfigurableEnvironment environment = new StandardEnvironment();
if (!ObjectUtils.isEmpty(config.getActiveProfiles())) {
setActiveProfiles(environment, config.getActiveProfiles());
}
TestPropertySourceUtils.addPropertiesFilesToEnvironment(environment, application.getResourceLoader() == null ? new DefaultResourceLoader(getClass().getClassLoader()) : application.getResourceLoader(), config.getPropertySourceLocations());
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, getInlinedProperties(config));
application.setEnvironment(environment);
List<ApplicationContextInitializer<?>> initializers = getInitializers(config, application);
if (config instanceof WebMergedContextConfiguration) {
application.setWebApplicationType(WebApplicationType.SERVLET);
if (!isEmbeddedWebEnvironment(config)) {
new WebConfigurer().configure(config, application, initializers);
}
} else if (config instanceof ReactiveWebMergedContextConfiguration) {
application.setWebApplicationType(WebApplicationType.REACTIVE);
if (!isEmbeddedWebEnvironment(config)) {
new ReactiveWebConfigurer().configure(application);
}
} else {
application.setWebApplicationType(WebApplicationType.NONE);
}
application.setInitializers(initializers);
ConfigurableApplicationContext context = application.run();
return context;
}
use of org.springframework.core.env.ConfigurableEnvironment in project spring-boot by spring-projects.
the class PropertyMappingContextCustomizerFactoryTests method getContextCustomizerWhenHasNoMappingShouldNotAddPropertySource.
@Test
public void getContextCustomizerWhenHasNoMappingShouldNotAddPropertySource() {
ContextCustomizer customizer = this.factory.createContextCustomizer(NoMapping.class, null);
ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class);
ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class);
given(context.getEnvironment()).willReturn(environment);
given(context.getBeanFactory()).willReturn(beanFactory);
customizer.customizeContext(context, null);
verifyZeroInteractions(environment);
}
use of org.springframework.core.env.ConfigurableEnvironment in project spring-boot by spring-projects.
the class SpringApplicationTests method disableCommandLinePropertySource.
@Test
public void disableCommandLinePropertySource() throws Exception {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.setAddCommandLineProperties(false);
ConfigurableEnvironment environment = new StandardEnvironment();
application.setEnvironment(environment);
this.context = application.run("--foo=bar");
assertThat(environment).doesNotHave(matchingPropertySource(PropertySource.class, "commandLineArgs"));
}
use of org.springframework.core.env.ConfigurableEnvironment in project spring-boot by spring-projects.
the class SpringApplicationTests method addProfiles.
@Test
public void addProfiles() throws Exception {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.setAdditionalProfiles("foo");
ConfigurableEnvironment environment = new StandardEnvironment();
application.setEnvironment(environment);
this.context = application.run();
assertThat(environment.acceptsProfiles("foo")).isTrue();
}
use of org.springframework.core.env.ConfigurableEnvironment in project spring-boot by spring-projects.
the class SpringApplicationTests method addProfilesOrderWithProperties.
@Test
public void addProfilesOrderWithProperties() throws Exception {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.setAdditionalProfiles("other");
ConfigurableEnvironment environment = new StandardEnvironment();
application.setEnvironment(environment);
this.context = application.run();
// Active profile should win over default
assertThat(environment.getProperty("my.property")).isEqualTo("fromotherpropertiesfile");
}
Aggregations