From 7279e97e5a200a0785fd320ec5ff55697b39ed48 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 21 May 2014 08:28:53 +0100 Subject: [PATCH] Add @IntegrationTest properties to Environment after system props Fixes gh-910 --- .../test/SpringApplicationContextLoader.java | 16 +++++++++++++++- .../SpringApplicationIntegrationTestTests.java | 10 +++++++++- .../src/test/resources/application.properties | 1 + 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java index 5fa353b83e7..30f542930b0 100644 --- a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java +++ b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java @@ -33,6 +33,9 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; import org.springframework.core.SpringVersion; import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.StandardEnvironment; import org.springframework.mock.web.MockServletContext; import org.springframework.test.context.ContextConfigurationAttributes; import org.springframework.test.context.ContextLoader; @@ -43,6 +46,7 @@ import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebMergedContextConfiguration; import org.springframework.util.ObjectUtils; import org.springframework.web.context.support.GenericWebApplicationContext; +import org.springframework.web.context.support.StandardServletEnvironment; /** * A {@link ContextLoader} that can be used to test Spring Boot applications (those that @@ -74,7 +78,17 @@ public class SpringApplicationContextLoader extends AbstractContextLoader { if (!ObjectUtils.isEmpty(config.getActiveProfiles())) { application.setAdditionalProfiles(config.getActiveProfiles()); } - application.setDefaultProperties(getEnvironmentProperties(config)); + ConfigurableEnvironment environment = new StandardEnvironment(); + if (config instanceof WebMergedContextConfiguration) { + environment = new StandardServletEnvironment(); + } + // Ensure @IntegrationTest properties go before external config and after system + environment.getPropertySources() + .addAfter( + StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, + new MapPropertySource("integrationTest", + getEnvironmentProperties(config))); + application.setEnvironment(environment); List> initializers = getInitializers(config, application); if (config instanceof WebMergedContextConfiguration) { diff --git a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestTests.java b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestTests.java index d9e86ef280c..a2457e88223 100644 --- a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestTests.java @@ -44,12 +44,15 @@ import static org.junit.Assert.assertNotEquals; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Config.class) @WebAppConfiguration -@IntegrationTest("server.port=0") +@IntegrationTest({ "server.port=0", "value=123" }) public class SpringApplicationIntegrationTestTests { @Value("${local.server.port}") private int port = 0; + @Value("${value}") + private int value = 0; + @Test public void runAndTestHttpEndpoint() { assertNotEquals(8080, this.port); @@ -59,6 +62,11 @@ public class SpringApplicationIntegrationTestTests { assertEquals("Hello World", body); } + @Test + public void annotationAttributesOverridePropertiesFile() throws Exception { + assertEquals(123, this.value); + } + @Configuration @EnableWebMvc @RestController diff --git a/spring-boot/src/test/resources/application.properties b/spring-boot/src/test/resources/application.properties index 64a23ced7ec..7cc1ee49061 100644 --- a/spring-boot/src/test/resources/application.properties +++ b/spring-boot/src/test/resources/application.properties @@ -1,3 +1,4 @@ foo: bucket +value: 1234 my.property: fromapplicationproperties sample.app.test.prop: *