Refine @TestPropertySource locations support
Update SpringApplicationContextLoader to configure test property locations before the ApplicationContext is actually started. Fixes gh-2198
This commit is contained in:
parent
80111c801a
commit
798e5f919d
|
@ -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<ApplicationContextInitializer<?>> getInitializers(
|
||||
MergedContextConfiguration mergedConfig, SpringApplication application) {
|
||||
List<ApplicationContextInitializer<?>> initializers = new ArrayList<ApplicationContextInitializer<?>>();
|
||||
initializers.add(new PropertySourceLocationsInitializer(mergedConfig
|
||||
.getPropertySourceLocations()));
|
||||
initializers.add(new ServerPortInfoApplicationContextInitializer());
|
||||
initializers.addAll(application.getInitializers());
|
||||
for (Class<? extends ApplicationContextInitializer<?>> 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<ConfigurableApplicationContext> {
|
||||
|
||||
private final String[] propertySourceLocations;
|
||||
|
||||
public PropertySourceLocationsInitializer(String[] propertySourceLocations) {
|
||||
this.propertySourceLocations = propertySourceLocations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(ConfigurableApplicationContext applicationContext) {
|
||||
TestPropertySourceUtils.addPropertiesFilesToEnvironment(applicationContext,
|
||||
this.propertySourceLocations);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue