Skip to content

Commit 54f334e

Browse files
committed
Add support for @TestPropertySource locations
Update SpringApplicationContextLoader to support @TestPropertySource location attributes. Fixes gh-2198
1 parent 1231da1 commit 54f334e

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.boot.context.web.ServletContextApplicationContextInitializer;
3434
import org.springframework.context.ApplicationContext;
3535
import org.springframework.context.ApplicationContextInitializer;
36+
import org.springframework.context.ConfigurableApplicationContext;
3637
import org.springframework.core.SpringVersion;
3738
import org.springframework.core.annotation.AnnotationUtils;
3839
import org.springframework.core.env.ConfigurableEnvironment;
@@ -44,6 +45,7 @@
4445
import org.springframework.test.context.MergedContextConfiguration;
4546
import org.springframework.test.context.support.AbstractContextLoader;
4647
import org.springframework.test.context.support.AnnotationConfigContextLoaderUtils;
48+
import org.springframework.test.context.support.TestPropertySourceUtils;
4749
import org.springframework.test.context.web.WebAppConfiguration;
4850
import org.springframework.test.context.web.WebMergedContextConfiguration;
4951
import org.springframework.util.Assert;
@@ -95,7 +97,10 @@ public ApplicationContext loadContext(MergedContextConfiguration config)
9597
application.setWebEnvironment(false);
9698
}
9799
application.setInitializers(initializers);
98-
return application.run();
100+
ConfigurableApplicationContext applicationContext = application.run();
101+
TestPropertySourceUtils.addPropertiesFilesToEnvironment(applicationContext,
102+
config.getPropertySourceLocations());
103+
return applicationContext;
99104
}
100105

101106
private void assertValidAnnotations(Class<?> testClass) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.SpringApplicationIntegrationTestTests.Config;
23+
import org.springframework.core.env.Environment;
24+
import org.springframework.test.context.TestPropertySource;
25+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
26+
import org.springframework.test.context.web.WebAppConfiguration;
27+
28+
import static org.hamcrest.Matchers.equalTo;
29+
import static org.junit.Assert.assertThat;
30+
31+
/**
32+
* Tests for {@link IntegrationTest} with {@link TestPropertySource} locations.
33+
*
34+
* @author Phillip Webb
35+
*/
36+
@RunWith(SpringJUnit4ClassRunner.class)
37+
@SpringApplicationConfiguration(classes = Config.class)
38+
@WebAppConfiguration
39+
@IntegrationTest({ "server.port=0", "value1=123" })
40+
@TestPropertySource(properties = "value2=456", locations = "classpath:/test-property-source-annotation.properties")
41+
public class SpringApplicationIntegrationTestPropertyLocationTests {
42+
43+
@Autowired
44+
private Environment environment;
45+
46+
@Test
47+
public void loadedProperties() throws Exception {
48+
assertThat(this.environment.getProperty("value1"), equalTo("123"));
49+
assertThat(this.environment.getProperty("value2"), equalTo("456"));
50+
assertThat(this.environment.getProperty("annotation-referenced"),
51+
equalTo("fromfile"));
52+
}
53+
54+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
annotation-referenced=fromfile

0 commit comments

Comments
 (0)