simple parent-child test
This commit is contained in:
parent
23c5dd64f9
commit
0e9b7b2684
1
spring-all/src/main/resources/child.properties
Normal file
1
spring-all/src/main/resources/child.properties
Normal file
@ -0,0 +1 @@
|
|||||||
|
child.name=child
|
1
spring-all/src/main/resources/parent.properties
Normal file
1
spring-all/src/main/resources/parent.properties
Normal file
@ -0,0 +1 @@
|
|||||||
|
parent.name=parent
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user