Search in sources :

Example 6 with ConfigurableEnvironment

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;
}
Also used : WebMergedContextConfiguration(org.springframework.test.context.web.WebMergedContextConfiguration) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) SpringApplication(org.springframework.boot.SpringApplication) ServletContextApplicationContextInitializer(org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer) ApplicationContextInitializer(org.springframework.context.ApplicationContextInitializer) StandardEnvironment(org.springframework.core.env.StandardEnvironment) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Example 7 with ConfigurableEnvironment

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);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) ContextCustomizer(org.springframework.test.context.ContextCustomizer) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Test(org.junit.Test)

Example 8 with ConfigurableEnvironment

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"));
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) StandardEnvironment(org.springframework.core.env.StandardEnvironment) CompositePropertySource(org.springframework.core.env.CompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) CommandLinePropertySource(org.springframework.core.env.CommandLinePropertySource) PropertySource(org.springframework.core.env.PropertySource) Test(org.junit.Test)

Example 9 with ConfigurableEnvironment

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();
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.Test)

Example 10 with ConfigurableEnvironment

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");
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.Test)

Aggregations

ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)54 Test (org.junit.Test)27 StandardEnvironment (org.springframework.core.env.StandardEnvironment)18 MutablePropertySources (org.springframework.core.env.MutablePropertySources)10 SpringApplication (org.springframework.boot.SpringApplication)7 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)6 MockEnvironment (org.springframework.mock.env.MockEnvironment)6 CompositePropertySource (org.springframework.core.env.CompositePropertySource)4 MapPropertySource (org.springframework.core.env.MapPropertySource)4 Environment (org.springframework.core.env.Environment)3 ProfileAnnotatedComponent (example.profilescan.ProfileAnnotatedComponent)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintStream (java.io.PrintStream)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 CommandLinePropertySource (org.springframework.core.env.CommandLinePropertySource)2 PropertySource (org.springframework.core.env.PropertySource)2