Search in sources :

Example 46 with TestBean

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

the class ContextLoaderTests method contextLoaderListenerWithProgrammaticAndGlobalInitializers.

@Test
void contextLoaderListenerWithProgrammaticAndGlobalInitializers() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
    sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, TestWebContextInitializer.class.getName());
    ContextLoaderListener listener = new ContextLoaderListener();
    listener.setContextInitializers(new TestContextInitializer());
    listener.contextInitialized(new ServletContextEvent(sc));
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
    TestBean testBean = wac.getBean(TestBean.class);
    assertThat(testBean.getName()).isEqualTo("testName");
    assertThat(wac.getServletContext().getAttribute("initialized")).isNotNull();
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) 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 47 with TestBean

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

the class ContextLoaderTests method contextLoaderListenerWithProgrammaticInitializers.

@Test
void contextLoaderListenerWithProgrammaticInitializers() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
    ContextLoaderListener listener = new ContextLoaderListener();
    listener.setContextInitializers(new TestContextInitializer(), new TestWebContextInitializer());
    listener.contextInitialized(new ServletContextEvent(sc));
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
    TestBean testBean = wac.getBean(TestBean.class);
    assertThat(testBean.getName()).isEqualTo("testName");
    assertThat(wac.getServletContext().getAttribute("initialized")).isNotNull();
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) 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 48 with TestBean

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

the class ContextLoaderTests method contextLoaderListenerWithMixedContextInitializers.

@Test
void contextLoaderListenerWithMixedContextInitializers() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
    sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, TestContextInitializer.class.getName());
    sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, TestWebContextInitializer.class.getName());
    ContextLoaderListener listener = new ContextLoaderListener();
    listener.contextInitialized(new ServletContextEvent(sc));
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
    TestBean testBean = wac.getBean(TestBean.class);
    assertThat(testBean.getName()).isEqualTo("testName");
    assertThat(wac.getServletContext().getAttribute("initialized")).isNotNull();
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) 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 49 with TestBean

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

the class ContextLoaderTests method classPathXmlApplicationContext.

@Test
@SuppressWarnings("resource")
void classPathXmlApplicationContext() throws IOException {
    ApplicationContext context = new ClassPathXmlApplicationContext("/org/springframework/web/context/WEB-INF/applicationContext.xml");
    assertThat(context.containsBean("father")).as("Has father").isTrue();
    assertThat(context.containsBean("rod")).as("Has rod").isTrue();
    assertThat(context.containsBean("kerry")).as("Hasn't kerry").isFalse();
    assertThat(((TestBean) context.getBean("rod")).getSpouse() == null).as("Doesn't have spouse").isTrue();
    assertThat("Roderick".equals(((TestBean) context.getBean("rod")).getName())).as("myinit not evaluated").isTrue();
    context = new ClassPathXmlApplicationContext(new String[] { "/org/springframework/web/context/WEB-INF/applicationContext.xml", "/org/springframework/web/context/WEB-INF/context-addition.xml" });
    assertThat(context.containsBean("father")).as("Has father").isTrue();
    assertThat(context.containsBean("rod")).as("Has rod").isTrue();
    assertThat(context.containsBean("kerry")).as("Has kerry").isTrue();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 50 with TestBean

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

the class CheckboxesTagTests method createTestBean.

@Override
protected TestBean createTestBean() {
    List colours = new ArrayList();
    colours.add(Colour.BLUE);
    colours.add(Colour.RED);
    colours.add(Colour.GREEN);
    List pets = new ArrayList();
    pets.add(new Pet("Rudiger"));
    pets.add(new Pet("Spot"));
    pets.add(new Pet("Fluffy"));
    pets.add(new Pet("Mufty"));
    Set someObjects = new HashSet();
    someObjects.add(new ItemPet("PET1"));
    someObjects.add(new ItemPet("PET2"));
    this.bean = new TestBean();
    this.bean.setDate(getDate());
    this.bean.setName("Rob Harrop");
    this.bean.setJedi(true);
    this.bean.setSomeBoolean(Boolean.TRUE);
    this.bean.setStringArray(new String[] { "bar", "foo" });
    this.bean.setSomeIntegerArray(new Integer[] { 2, 1 });
    this.bean.setOtherColours(colours);
    this.bean.setPets(pets);
    this.bean.setSomeSet(someObjects);
    List list = new ArrayList();
    list.add("foo");
    list.add("bar");
    this.bean.setSomeList(list);
    return this.bean;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) TestBean(org.springframework.beans.testfixture.beans.TestBean) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Pet(org.springframework.beans.testfixture.beans.Pet) HashSet(java.util.HashSet)

Aggregations

TestBean (org.springframework.beans.testfixture.beans.TestBean)808 Test (org.junit.jupiter.api.Test)765 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)472 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)268 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)183 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)167 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)162 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)118 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)96 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)40 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)40 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)39 Properties (java.util.Properties)38 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)37 HashMap (java.util.HashMap)36 Errors (org.springframework.validation.Errors)35 PropertyEditorSupport (java.beans.PropertyEditorSupport)34 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)29 List (java.util.List)28 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)28