|
| 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 | +} |
0 commit comments