Search in sources :

Example 16 with MockEnvironment

use of org.springframework.mock.env.MockEnvironment in project spring-boot by spring-projects.

the class LogbackLoggingSystemTests method testLevelPatternProperty.

@Test
public void testLevelPatternProperty() {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("logging.pattern.level", "X%clr(%p)X");
    LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(environment);
    this.loggingSystem.initialize(loggingInitializationContext, null, null);
    this.logger.info("Hello world");
    String output = this.output.toString().trim();
    assertThat(getLineWithText(output, "Hello world")).contains("XINFOX");
}
Also used : LoggingInitializationContext(org.springframework.boot.logging.LoggingInitializationContext) MockEnvironment(org.springframework.mock.env.MockEnvironment) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 17 with MockEnvironment

use of org.springframework.mock.env.MockEnvironment in project spring-framework by spring-projects.

the class PropertySourcesPlaceholderConfigurerTests method optionalPropertyWithValue.

@Test
public void optionalPropertyWithValue() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setConversionService(new DefaultConversionService());
    bf.registerBeanDefinition("testBean", genericBeanDefinition(OptionalTestBean.class).addPropertyValue("name", "${my.name}").getBeanDefinition());
    MockEnvironment env = new MockEnvironment();
    env.setProperty("my.name", "myValue");
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    ppc.setEnvironment(env);
    ppc.setIgnoreUnresolvablePlaceholders(true);
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(OptionalTestBean.class).getName(), equalTo(Optional.of("myValue")));
}
Also used : MockEnvironment(org.springframework.mock.env.MockEnvironment) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Test(org.junit.Test)

Example 18 with MockEnvironment

use of org.springframework.mock.env.MockEnvironment in project spring-framework by spring-projects.

the class PropertySourcesPlaceholderConfigurerTests method withNonEnumerablePropertySource.

@Test
public void withNonEnumerablePropertySource() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class).addPropertyValue("name", "${foo}").getBeanDefinition());
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    PropertySource<?> ps = new PropertySource<Object>("simplePropertySource", new Object()) {

        @Override
        public Object getProperty(String key) {
            return "bar";
        }
    };
    MockEnvironment env = new MockEnvironment();
    env.getPropertySources().addFirst(ps);
    ppc.setEnvironment(env);
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(TestBean.class).getName(), equalTo("bar"));
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) MockEnvironment(org.springframework.mock.env.MockEnvironment) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PropertySource(org.springframework.core.env.PropertySource) MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Example 19 with MockEnvironment

use of org.springframework.mock.env.MockEnvironment in project spring-framework by spring-projects.

the class PropertySourcesPlaceholderConfigurerTests method trimValuesIsOffByDefault.

@Test
public void trimValuesIsOffByDefault() {
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerBeanDefinition("testBean", rootBeanDefinition(TestBean.class).addPropertyValue("name", "${my.name}").getBeanDefinition());
    ppc.setEnvironment(new MockEnvironment().withProperty("my.name", " myValue  "));
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(TestBean.class).getName(), equalTo(" myValue  "));
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) MockEnvironment(org.springframework.mock.env.MockEnvironment) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Example 20 with MockEnvironment

use of org.springframework.mock.env.MockEnvironment in project spring-framework by spring-projects.

the class PropertySourcesPlaceholderConfigurerTests method explicitPropertySourcesExcludesEnvironment.

@Test
public void explicitPropertySourcesExcludesEnvironment() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class).addPropertyValue("name", "${my.name}").getBeanDefinition());
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new MockPropertySource());
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    ppc.setPropertySources(propertySources);
    ppc.setEnvironment(new MockEnvironment().withProperty("my.name", "env"));
    ppc.setIgnoreUnresolvablePlaceholders(true);
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
    assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) MockEnvironment(org.springframework.mock.env.MockEnvironment) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) MutablePropertySources(org.springframework.core.env.MutablePropertySources) MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Aggregations

MockEnvironment (org.springframework.mock.env.MockEnvironment)63 Test (org.junit.Test)53 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)10 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)9 TestBean (org.springframework.tests.sample.beans.TestBean)7 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)6 LoggingInitializationContext (org.springframework.boot.logging.LoggingInitializationContext)5 CoffeeNetConfigurationProperties (coffee.synyx.autoconfigure.CoffeeNetConfigurationProperties)4 URL (java.net.URL)4 Before (org.junit.Before)4 ServerProperties (org.springframework.boot.autoconfigure.web.ServerProperties)4 InetUtils (org.springframework.cloud.commons.util.InetUtils)4 InetUtilsProperties (org.springframework.cloud.commons.util.InetUtilsProperties)4 Environment (org.springframework.core.env.Environment)4 Matchers.containsString (org.hamcrest.Matchers.containsString)3 MutablePropertySources (org.springframework.core.env.MutablePropertySources)3 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 WebConnection (com.gargoylesoftware.htmlunit.WebConnection)2 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2