Search in sources :

Example 1 with LifecycleBean

use of org.springframework.beans.testfixture.beans.LifecycleBean in project spring-framework by spring-projects.

the class XmlListableBeanFactoryTests method lifecycleMethods.

@Test
public void lifecycleMethods() {
    LifecycleBean bean = (LifecycleBean) getBeanFactory().getBean("lifecycle");
    bean.businessMethod();
}
Also used : LifecycleBean(org.springframework.beans.testfixture.beans.LifecycleBean) Test(org.junit.jupiter.api.Test)

Example 2 with LifecycleBean

use of org.springframework.beans.testfixture.beans.LifecycleBean in project spring-framework by spring-projects.

the class ContextLoaderTests method contextLoaderListenerWithDefaultContext.

@Test
void contextLoaderListenerWithDefaultContext() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/applicationContext.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml");
    ServletContextListener listener = new ContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);
    String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
    WebApplicationContext context = (WebApplicationContext) sc.getAttribute(contextAttr);
    boolean condition1 = context instanceof XmlWebApplicationContext;
    assertThat(condition1).as("Correct WebApplicationContext exposed in ServletContext").isTrue();
    assertThat(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext).isTrue();
    LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
    assertThat(context.containsBean("father")).as("Has father").isTrue();
    assertThat(context.containsBean("rod")).as("Has rod").isTrue();
    assertThat(context.containsBean("kerry")).as("Has kerry").isTrue();
    boolean condition = !lb.isDestroyed();
    assertThat(condition).as("Not destroyed").isTrue();
    assertThat(context.containsBean("beans1.bean1")).isFalse();
    assertThat(context.containsBean("beans1.bean2")).isFalse();
    listener.contextDestroyed(event);
    assertThat(lb.isDestroyed()).as("Destroyed").isTrue();
    assertThat(sc.getAttribute(contextAttr)).isNull();
    assertThat(WebApplicationContextUtils.getWebApplicationContext(sc)).isNull();
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) ServletContextListener(jakarta.servlet.ServletContextListener) LifecycleBean(org.springframework.beans.testfixture.beans.LifecycleBean) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletContextEvent(jakarta.servlet.ServletContextEvent) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 3 with LifecycleBean

use of org.springframework.beans.testfixture.beans.LifecycleBean in project spring-framework by spring-projects.

the class AbstractApplicationContextTests method closeTriggersDestroy.

@Test
public void closeTriggersDestroy() {
    LifecycleBean lb = (LifecycleBean) applicationContext.getBean("lifecycle");
    boolean condition = !lb.isDestroyed();
    assertThat(condition).as("Not destroyed").isTrue();
    applicationContext.close();
    if (applicationContext.getParent() != null) {
        ((ConfigurableApplicationContext) applicationContext.getParent()).close();
    }
    assertThat(lb.isDestroyed()).as("Destroyed").isTrue();
    applicationContext.close();
    if (applicationContext.getParent() != null) {
        ((ConfigurableApplicationContext) applicationContext.getParent()).close();
    }
    assertThat(lb.isDestroyed()).as("Destroyed").isTrue();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) LifecycleBean(org.springframework.beans.testfixture.beans.LifecycleBean) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)3 LifecycleBean (org.springframework.beans.testfixture.beans.LifecycleBean)3 ServletContextEvent (jakarta.servlet.ServletContextEvent)1 ServletContextListener (jakarta.servlet.ServletContextListener)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)1 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)1 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)1