Remove SystemProperties from tests in strict mode

Remove system properties when testing strict property binding. This
prevents test failures that can occur if the local environment happens
to have certain environment variables defined.
pull/172/head
Phillip Webb 11 years ago
parent 60cb5fd35c
commit 983ef16eae

@ -34,6 +34,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindException;
@ -101,6 +102,7 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testStrictPropertiesBinding() {
removeSystemProperties();
this.context.register(StrictTestConfiguration.class);
TestUtils.addEnviroment(this.context, "name:foo");
this.context.refresh();
@ -119,9 +121,9 @@ public class EnableConfigurationPropertiesTests {
assertEquals("foo", this.context.getBean(TestProperties.class).name);
}
@Test
public void testIgnoreNestedPropertiesBinding() {
removeSystemProperties();
this.context.register(IgnoreNestedTestConfiguration.class);
TestUtils.addEnviroment(this.context, "name:foo", "nested.name:bar");
this.context.refresh();
@ -288,6 +290,17 @@ public class EnableConfigurationPropertiesTests {
assertEquals("foo", this.context.getBean(TestConsumer.class).getName());
}
/**
* Strict tests need a known set of properties so we remove system items which may be
* environment specific.
*/
private void removeSystemProperties() {
MutablePropertySources sources = this.context.getEnvironment()
.getPropertySources();
sources.remove("systemProperties");
sources.remove("systemEnvironment");
}
@Configuration
@EnableConfigurationProperties(TestProperties.class)
protected static class TestConfiguration {
@ -302,6 +315,7 @@ public class EnableConfigurationPropertiesTests {
@EnableConfigurationProperties(EmbeddedTestProperties.class)
protected static class EmbeddedTestConfiguration {
}
@Configuration
@EnableConfigurationProperties(IgnoreNestedTestProperties.class)
protected static class IgnoreNestedTestConfiguration {
@ -424,8 +438,8 @@ public class EnableConfigurationPropertiesTests {
@ConfigurationProperties(ignoreUnknownFields = false)
protected static class StrictTestProperties extends TestProperties {
}
@ConfigurationProperties(name = "spring.foo")
protected static class EmbeddedTestProperties extends TestProperties {

Loading…
Cancel
Save