From a37ae5d55629b95dcfbf87e7aa3a60354d017670 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 7 Apr 2016 11:34:09 +0100 Subject: [PATCH] Polish test property source changes made in 69b08291 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TestPropertySourcesInitializer needs to be ordered with a high precedence, specifically higher than ContextIdApplicationContextInitializer, to ensure that any properties used in setting the context’s id are available in the environment. Closes gh-4828 --- .../boot/test/context/SpringBootContextLoader.java | 11 +++++++++-- ...plicationIntegrationTestPropertyLocationTests.java | 8 ++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java index aff052911b..a7a3ddb7e3 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java @@ -31,6 +31,7 @@ import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.Ordered; import org.springframework.core.SpringVersion; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.env.ConfigurableEnvironment; @@ -245,8 +246,9 @@ public class SpringBootContextLoader extends AbstractContextLoader { /** * {@link ApplicationContextInitializer} to set up test property sources. */ - private static class TestPropertySourcesInitializer - implements ApplicationContextInitializer { + + private static class TestPropertySourcesInitializer implements + ApplicationContextInitializer, Ordered { private final String[] propertySourceLocations; @@ -266,6 +268,11 @@ public class SpringBootContextLoader extends AbstractContextLoader { this.inlinedProperties); } + @Override + public int getOrder() { + return Ordered.HIGHEST_PRECEDENCE + 10; + } + } /** diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestPropertyLocationTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestPropertyLocationTests.java index 2e8abb386b..f71aed8440 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestPropertyLocationTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestPropertyLocationTests.java @@ -55,8 +55,8 @@ public class SpringApplicationIntegrationTestPropertyLocationTests { public void loadedProperties() throws Exception { assertThat(this.environment.getProperty("value1")).isEqualTo("123"); assertThat(this.environment.getProperty("value2")).isEqualTo("456"); - assertThat(this.environment.getProperty("annotation-referenced")) - .isEqualTo("fromfile"); + assertThat(this.environment.getProperty("property-source-location")) + .isEqualTo("baz"); } @Configuration @@ -68,14 +68,14 @@ public class SpringApplicationIntegrationTestPropertyLocationTests { @Value("${value2}") private String value2; - @Value("${annotation-referenced}") + @Value("${property-source-location}") private String annotationReferenced; @PostConstruct void checkValues() { assertThat(this.value1).isEqualTo("123"); assertThat(this.value2).isEqualTo("456"); - assertThat(this.annotationReferenced).isEqualTo("fromfile"); + assertThat(this.annotationReferenced).isEqualTo("baz"); } }