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 69992ff053..a821c78142 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 @@ -76,7 +76,7 @@ public class SpringApplicationContextLoader extends AbstractContextLoader { private static final String LINE_SEPARATOR = System.getProperty("line.separator"); @Override - public ApplicationContext loadContext(MergedContextConfiguration config) + public ApplicationContext loadContext(final MergedContextConfiguration config) throws Exception { assertValidAnnotations(config.getTestClass()); SpringApplication application = getSpringApplication(); @@ -98,8 +98,6 @@ public class SpringApplicationContextLoader extends AbstractContextLoader { } application.setInitializers(initializers); ConfigurableApplicationContext applicationContext = application.run(); - TestPropertySourceUtils.addPropertiesFilesToEnvironment(applicationContext, - config.getPropertySourceLocations()); return applicationContext; } @@ -116,7 +114,7 @@ public class SpringApplicationContextLoader extends AbstractContextLoader { /** * Builds new {@link org.springframework.boot.SpringApplication} instance. You can - * override this method to add custom behaviour + * override this method to add custom behavior * @return {@link org.springframework.boot.SpringApplication} instance */ protected SpringApplication getSpringApplication() { @@ -196,6 +194,8 @@ public class SpringApplicationContextLoader extends AbstractContextLoader { private List> getInitializers( MergedContextConfiguration mergedConfig, SpringApplication application) { List> initializers = new ArrayList>(); + initializers.add(new PropertySourceLocationsInitializer(mergedConfig + .getPropertySourceLocations())); initializers.add(new ServerPortInfoApplicationContextInitializer()); initializers.addAll(application.getInitializers()); for (Class> initializerClass : mergedConfig @@ -273,4 +273,24 @@ public class SpringApplicationContextLoader extends AbstractContextLoader { .findAnnotation(testClass, WebIntegrationTest.class) != null)); } + /** + * {@link ApplicationContextInitializer} to setup test property source locations. + */ + private static class PropertySourceLocationsInitializer implements + ApplicationContextInitializer { + + private final String[] propertySourceLocations; + + public PropertySourceLocationsInitializer(String[] propertySourceLocations) { + this.propertySourceLocations = propertySourceLocations; + } + + @Override + public void initialize(ConfigurableApplicationContext applicationContext) { + TestPropertySourceUtils.addPropertiesFilesToEnvironment(applicationContext, + this.propertySourceLocations); + } + + } + } diff --git a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestPropertyLocationTests.java b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestPropertyLocationTests.java index be2364d07d..fdf1265020 100644 --- a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestPropertyLocationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestPropertyLocationTests.java @@ -16,10 +16,15 @@ package org.springframework.boot.test; +import javax.annotation.PostConstruct; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.SpringApplicationIntegrationTestPropertyLocationTests.MoreConfig; import org.springframework.boot.test.SpringApplicationIntegrationTestTests.Config; +import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -34,7 +39,7 @@ import static org.junit.Assert.assertThat; * @author Phillip Webb */ @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = Config.class) +@SpringApplicationConfiguration(classes = { Config.class, MoreConfig.class }) @WebAppConfiguration @IntegrationTest({ "server.port=0", "value1=123" }) @TestPropertySource(properties = "value2=456", locations = "classpath:/test-property-source-annotation.properties") @@ -51,4 +56,25 @@ public class SpringApplicationIntegrationTestPropertyLocationTests { equalTo("fromfile")); } + @Configuration + static class MoreConfig { + + @Value("${value1}") + private String value1; + + @Value("${value2}") + private String value2; + + @Value("${annotation-referenced}") + private String annotationReferenced; + + @PostConstruct + void checkValues() { + assertThat(this.value1, equalTo("123")); + assertThat(this.value2, equalTo("456")); + assertThat(this.annotationReferenced, equalTo("fromfile")); + } + + } + }