Search in sources :

Example 16 with DerivedTestBean

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

the class DefaultListableBeanFactoryTests method prototypeWithArrayConversionForFactoryMethod.

@Test
void prototypeWithArrayConversionForFactoryMethod() {
    List<String> list = ManagedList.of("myName", "myBeanName");
    RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
    bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bd.setFactoryMethodName("create");
    bd.getConstructorArgumentValues().addGenericArgumentValue(list);
    lbf.registerBeanDefinition("test", bd);
    DerivedTestBean tb = (DerivedTestBean) lbf.getBean("test");
    assertThat(tb.getName()).isEqualTo("myName");
    assertThat(tb.getBeanName()).isEqualTo("myBeanName");
    DerivedTestBean tb2 = (DerivedTestBean) lbf.getBean("test");
    assertThat(tb != tb2).isTrue();
    assertThat(tb2.getName()).isEqualTo("myName");
    assertThat(tb2.getBeanName()).isEqualTo("myBeanName");
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Example 17 with DerivedTestBean

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

the class BeanUtilsTests method copyPropertiesWithDifferentTypes2.

@Test
void copyPropertiesWithDifferentTypes2() throws Exception {
    TestBean tb = new TestBean();
    tb.setName("rod");
    tb.setAge(32);
    tb.setTouchy("touchy");
    DerivedTestBean tb2 = new DerivedTestBean();
    assertThat(tb2.getName() == null).as("Name empty").isTrue();
    assertThat(tb2.getAge() == 0).as("Age empty").isTrue();
    assertThat(tb2.getTouchy() == null).as("Touchy empty").isTrue();
    BeanUtils.copyProperties(tb, tb2);
    assertThat(tb2.getName().equals(tb.getName())).as("Name copied").isTrue();
    assertThat(tb2.getAge() == tb.getAge()).as("Age copied").isTrue();
    assertThat(tb2.getTouchy().equals(tb.getTouchy())).as("Touchy copied").isTrue();
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 18 with DerivedTestBean

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

the class BeanUtilsTests method copyPropertiesWithDifferentTypes1.

@Test
void copyPropertiesWithDifferentTypes1() throws Exception {
    DerivedTestBean tb = new DerivedTestBean();
    tb.setName("rod");
    tb.setAge(32);
    tb.setTouchy("touchy");
    TestBean tb2 = new TestBean();
    assertThat(tb2.getName() == null).as("Name empty").isTrue();
    assertThat(tb2.getAge() == 0).as("Age empty").isTrue();
    assertThat(tb2.getTouchy() == null).as("Touchy empty").isTrue();
    BeanUtils.copyProperties(tb, tb2);
    assertThat(tb2.getName().equals(tb.getName())).as("Name copied").isTrue();
    assertThat(tb2.getAge() == tb.getAge()).as("Age copied").isTrue();
    assertThat(tb2.getTouchy().equals(tb.getTouchy())).as("Touchy copied").isTrue();
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 19 with DerivedTestBean

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

the class DefaultListableBeanFactoryTests method createBeanWithDisposableBean.

@Test
void createBeanWithDisposableBean() {
    DerivedTestBean tb = lbf.createBean(DerivedTestBean.class);
    assertThat(tb.getBeanFactory()).isSameAs(lbf);
    lbf.destroyBean(tb);
    assertThat(tb.wasDestroyed()).isTrue();
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Example 20 with DerivedTestBean

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

the class XmlBeanFactoryTests method inheritanceWithDifferentClass.

@Test
void inheritanceWithDifferentClass() {
    DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
    DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
    new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
    assertThat(child.getType("inheritsWithClass")).isEqualTo(DerivedTestBean.class);
    DerivedTestBean inherits = (DerivedTestBean) child.getBean("inheritsWithDifferentClass");
    // Name property value is overridden
    assertThat(inherits.getName().equals("override")).isTrue();
    // Age property is inherited from bean in parent factory
    assertThat(inherits.getAge() == 1).isTrue();
    assertThat(inherits.wasInitialized()).isTrue();
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Aggregations

DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)27 Test (org.junit.jupiter.api.Test)26 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)7 TestBean (org.springframework.beans.testfixture.beans.TestBean)7 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)6 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)6 PropertyEditorSupport (java.beans.PropertyEditorSupport)4 WebApplicationContext (org.springframework.web.context.WebApplicationContext)3 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)2 MockHttpSession (org.springframework.web.testfixture.servlet.MockHttpSession)2 ServletContextEvent (jakarta.servlet.ServletContextEvent)1 InputStream (java.io.InputStream)1 Serializable (java.io.Serializable)1