diff --git a/spring-all/src/main/resources/child.properties b/spring-all/src/main/resources/child.properties new file mode 100644 index 0000000000..bde8f83e7f --- /dev/null +++ b/spring-all/src/main/resources/child.properties @@ -0,0 +1 @@ +child.name=child \ No newline at end of file diff --git a/spring-all/src/main/resources/parent.properties b/spring-all/src/main/resources/parent.properties new file mode 100644 index 0000000000..b9011a20d2 --- /dev/null +++ b/spring-all/src/main/resources/parent.properties @@ -0,0 +1 @@ +parent.name=parent \ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/properties/config/ChildConfig.java b/spring-all/src/test/java/org/baeldung/properties/config/ChildConfig.java new file mode 100644 index 0000000000..6a33765748 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/properties/config/ChildConfig.java @@ -0,0 +1,10 @@ +package org.baeldung.properties.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +@Configuration +@PropertySource("classpath:child.properties") +public class ChildConfig { + +} diff --git a/spring-all/src/test/java/org/baeldung/properties/config/ParentChildPropertiesTest.java b/spring-all/src/test/java/org/baeldung/properties/config/ParentChildPropertiesTest.java new file mode 100644 index 0000000000..11c62e21b6 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/properties/config/ParentChildPropertiesTest.java @@ -0,0 +1,35 @@ +package org.baeldung.properties.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.ContextHierarchy; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.context.WebApplicationContext; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextHierarchy({ @ContextConfiguration(classes = ParentConfig.class), @ContextConfiguration(classes = ChildConfig.class) }) +public class ParentChildPropertiesTest { + + @Autowired + private WebApplicationContext wac; + + @Test + public void givenParentPropertySource_whenGetValue_thenCorrect() { + final Environment childEnv = wac.getEnvironment(); + final Environment parentEnv = wac.getParent().getEnvironment(); + + assertEquals(parentEnv.getProperty("parent.name"), "parent"); + assertNull(parentEnv.getProperty("child.name")); + + assertEquals(childEnv.getProperty("parent.name"), "parent"); + assertEquals(childEnv.getProperty("child.name"), "child"); + } +} diff --git a/spring-all/src/test/java/org/baeldung/properties/config/ParentConfig.java b/spring-all/src/test/java/org/baeldung/properties/config/ParentConfig.java new file mode 100644 index 0000000000..2279af7803 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/properties/config/ParentConfig.java @@ -0,0 +1,10 @@ +package org.baeldung.properties.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +@Configuration +@PropertySource("classpath:parent.properties") +public class ParentConfig { + +}