Add @IntegrationTest properties to Environment after system props

Fixes gh-910
This commit is contained in:
Dave Syer
2014-05-21 08:28:53 +01:00
parent f123fd5a4a
commit 7279e97e5a
3 changed files with 25 additions and 2 deletions

View File

@@ -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<ApplicationContextInitializer<?>> initializers = getInitializers(config,
application);
if (config instanceof WebMergedContextConfiguration) {

View File

@@ -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

View File

@@ -1,3 +1,4 @@
foo: bucket
value: 1234
my.property: fromapplicationproperties
sample.app.test.prop: *