simple parent-child test

This commit is contained in:
DOHA 2015-07-10 22:19:31 +02:00
parent 23c5dd64f9
commit 0e9b7b2684
5 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1 @@
child.name=child

View File

@ -0,0 +1 @@
parent.name=parent

View File

@ -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 {
}

View File

@ -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");
}
}

View File

@ -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 {
}