Skip to content

Commit 0e9b7b2

Browse files
committed
simple parent-child test
1 parent 23c5dd6 commit 0e9b7b2

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
child.name=child
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
parent.name=parent
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.baeldung.properties.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.context.annotation.PropertySource;
5+
6+
@Configuration
7+
@PropertySource("classpath:child.properties")
8+
public class ChildConfig {
9+
10+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.baeldung.properties.config;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
5+
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.core.env.Environment;
10+
import org.springframework.test.context.ContextConfiguration;
11+
import org.springframework.test.context.ContextHierarchy;
12+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13+
import org.springframework.test.context.web.WebAppConfiguration;
14+
import org.springframework.web.context.WebApplicationContext;
15+
16+
@RunWith(SpringJUnit4ClassRunner.class)
17+
@WebAppConfiguration
18+
@ContextHierarchy({ @ContextConfiguration(classes = ParentConfig.class), @ContextConfiguration(classes = ChildConfig.class) })
19+
public class ParentChildPropertiesTest {
20+
21+
@Autowired
22+
private WebApplicationContext wac;
23+
24+
@Test
25+
public void givenParentPropertySource_whenGetValue_thenCorrect() {
26+
final Environment childEnv = wac.getEnvironment();
27+
final Environment parentEnv = wac.getParent().getEnvironment();
28+
29+
assertEquals(parentEnv.getProperty("parent.name"), "parent");
30+
assertNull(parentEnv.getProperty("child.name"));
31+
32+
assertEquals(childEnv.getProperty("parent.name"), "parent");
33+
assertEquals(childEnv.getProperty("child.name"), "child");
34+
}
35+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.baeldung.properties.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.context.annotation.PropertySource;
5+
6+
@Configuration
7+
@PropertySource("classpath:parent.properties")
8+
public class ParentConfig {
9+
10+
}

0 commit comments

Comments
 (0)