Add @IntegrationTest properties to Environment after system props
Fixes gh-910
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
foo: bucket
|
||||
value: 1234
|
||||
my.property: fromapplicationproperties
|
||||
sample.app.test.prop: *
|
||||
|
||||
Reference in New Issue
Block a user