|
| 1 | +package org.baeldung.properties.parentchild; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNull; |
| 5 | + |
| 6 | +import org.baeldung.properties.parentchild.config.ChildConfig2; |
| 7 | +import org.baeldung.properties.parentchild.config.ParentConfig2; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.core.env.Environment; |
| 12 | +import org.springframework.test.context.ContextConfiguration; |
| 13 | +import org.springframework.test.context.ContextHierarchy; |
| 14 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 15 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 16 | +import org.springframework.web.context.WebApplicationContext; |
| 17 | + |
| 18 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 19 | +@WebAppConfiguration |
| 20 | +@ContextHierarchy({ @ContextConfiguration(classes = ParentConfig2.class), @ContextConfiguration(classes = ChildConfig2.class) }) |
| 21 | +public class ParentChildPropertyPlaceHolderPropertiesTest { |
| 22 | + |
| 23 | + @Autowired |
| 24 | + private WebApplicationContext wac; |
| 25 | + |
| 26 | + @Test |
| 27 | + public void givenPropertyPlaceHolder_whenGetPropertyUsingEnv_thenCorrect() { |
| 28 | + final Environment childEnv = wac.getEnvironment(); |
| 29 | + final Environment parentEnv = wac.getParent().getEnvironment(); |
| 30 | + |
| 31 | + assertNull(parentEnv.getProperty("parent.name")); |
| 32 | + assertNull(parentEnv.getProperty("child.name")); |
| 33 | + |
| 34 | + assertNull(childEnv.getProperty("parent.name")); |
| 35 | + assertNull(childEnv.getProperty("child.name")); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void givenPropertyPlaceHolder_whenGetPropertyUsingValueAnnotation_thenCorrect() { |
| 40 | + final ChildValueHolder childValueHolder = wac.getBean(ChildValueHolder.class); |
| 41 | + final ParentValueHolder parentValueHolder = wac.getParent().getBean(ParentValueHolder.class); |
| 42 | + |
| 43 | + assertEquals(parentValueHolder.getParentName(), "parent"); |
| 44 | + assertEquals(parentValueHolder.getChildName(), "-"); |
| 45 | + |
| 46 | + assertEquals(childValueHolder.getParentName(), "-"); |
| 47 | + assertEquals(childValueHolder.getChildName(), "child"); |
| 48 | + } |
| 49 | + |
| 50 | +} |
0 commit comments