DEV Community

Salad Lam
Salad Lam

Posted on

Create parent-child ApplicationContext in Spring Framework test

Setup

Bean class

package test;

public class ParentClass {
    @Override
    public String toString() {
        return "ParentClass " + super.toString() + " {}";
    }
}

public class ChildClass {
    private ParentClass parent;

    public void setParent(ParentClass parent) {
        this.parent = parent;
    }

    @Override
    public String toString() {
        return "ChildClass " + super.toString() + " {" +
                "parent=" + parent +
                '}';
    }
}
Enter fullscreen mode Exit fullscreen mode

parent.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="beanParent" class="test.ParentClass">
    </bean>
</beans>
Enter fullscreen mode Exit fullscreen mode

child.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="beanChild" class="test.ChildClass">
        <property name="parent" ref="beanParent"/>
    </bean>
</beans>
Enter fullscreen mode Exit fullscreen mode

Test class

package test;

import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(SpringExtension.class)
@ContextHierarchy({
        @ContextConfiguration(name = "parent", locations = {"classpath:/parent.xml"}),
        @ContextConfiguration(name = "child", locations = {"classpath:/child.xml"}),
})
@TestMethodOrder(MethodOrderer.MethodName.class)
// re-create child ApplicationContext after each test method
@DirtiesContext(
        classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD,
        hierarchyMode = DirtiesContext.HierarchyMode.CURRENT_LEVEL
)
class ContextHierarchyTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(ContextHierarchyTest.class);

    private ApplicationContext applicationContext;

    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext) {
        LOGGER.info("ApplicationContext injected");
        this.applicationContext = applicationContext;
    }

    @Test
    void test1() {
        ApplicationContext parentApplicationContext = applicationContext.getParent();
        assertThat(parentApplicationContext).isNotNull();

        assertThat(parentApplicationContext.getBean("beanParent")).isEqualTo(applicationContext.getBean("beanParent"));
        String parentBean = applicationContext.getBean("beanParent").toString();
        String childBean = applicationContext.getBean("beanChild").toString();
        LOGGER.info("parent={}, child={}", parentBean, childBean);
    }

    @Test
    void test2() {
        LOGGER.info("parent={}, child={}", applicationContext.getBean("beanParent"), applicationContext.getBean("beanChild"));
    }

}
Enter fullscreen mode Exit fullscreen mode

Screen output

ApplicationContext injected
parent=ParentClass test.ParentClass@314ed053 {}, child=ChildClass test.ChildClass@35088e87 {parent=ParentClass test.ParentClass@314ed053 {}}
ApplicationContext injected
parent=ParentClass test.ParentClass@314ed053 {}, child=ChildClass test.ChildClass@7a231dfd {parent=ParentClass test.ParentClass@314ed053 {}}
Enter fullscreen mode Exit fullscreen mode
  1. when a bean cannot be found in child ApplicationContext, will query its parent ApplicationContext
  2. child ApplicationContext will be re-create after each test method, beans in child ApplicationContext will be re-create also. But parent ApplicationContext not be re-created

Top comments (0)