Search in sources :

Example 16 with DependenciesBean

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

the class XmlBeanFactoryTests method autowireWithDefault.

@Test
void autowireWithDefault() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(DEFAULT_AUTOWIRE_CONTEXT);
    DependenciesBean rod1 = (DependenciesBean) xbf.getBean("rod1");
    // should have been autowired
    assertThat(rod1.getSpouse()).isNotNull();
    assertThat(rod1.getSpouse().getName().equals("Kerry")).isTrue();
    DependenciesBean rod2 = (DependenciesBean) xbf.getBean("rod2");
    // should have been autowired
    assertThat(rod2.getSpouse()).isNotNull();
    assertThat(rod2.getSpouse().getName().equals("Kerry")).isTrue();
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DependenciesBean(org.springframework.beans.testfixture.beans.DependenciesBean) Test(org.junit.jupiter.api.Test)

Example 17 with DependenciesBean

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

the class XmlBeanFactoryTests method doTestAutowire.

private void doTestAutowire(DefaultListableBeanFactory xbf) {
    DependenciesBean rod1 = (DependenciesBean) xbf.getBean("rod1");
    TestBean kerry = (TestBean) xbf.getBean("spouse");
    // should have been autowired
    assertThat(rod1.getSpouse()).isEqualTo(kerry);
    DependenciesBean rod1a = (DependenciesBean) xbf.getBean("rod1a");
    // should have been autowired
    assertThat(rod1a.getSpouse()).isEqualTo(kerry);
    DependenciesBean rod2 = (DependenciesBean) xbf.getBean("rod2");
    // should have been autowired
    assertThat(rod2.getSpouse()).isEqualTo(kerry);
    DependenciesBean rod2a = (DependenciesBean) xbf.getBean("rod2a");
    // should have been set explicitly
    assertThat(rod2a.getSpouse()).isEqualTo(kerry);
    ConstructorDependenciesBean rod3 = (ConstructorDependenciesBean) xbf.getBean("rod3");
    IndexedTestBean other = (IndexedTestBean) xbf.getBean("other");
    // should have been autowired
    assertThat(rod3.getSpouse1()).isEqualTo(kerry);
    assertThat(rod3.getSpouse2()).isEqualTo(kerry);
    assertThat(rod3.getOther()).isEqualTo(other);
    ConstructorDependenciesBean rod3a = (ConstructorDependenciesBean) xbf.getBean("rod3a");
    // should have been autowired
    assertThat(rod3a.getSpouse1()).isEqualTo(kerry);
    assertThat(rod3a.getSpouse2()).isEqualTo(kerry);
    assertThat(rod3a.getOther()).isEqualTo(other);
    assertThatExceptionOfType(FatalBeanException.class).isThrownBy(() -> xbf.getBean("rod4", ConstructorDependenciesBean.class));
    DependenciesBean rod5 = (DependenciesBean) xbf.getBean("rod5");
    // Should not have been autowired
    assertThat((Object) rod5.getSpouse()).isNull();
    BeanFactory appCtx = (BeanFactory) xbf.getBean("childAppCtx");
    assertThat(appCtx.containsBean("rod1")).isTrue();
    assertThat(appCtx.containsBean("jenny")).isTrue();
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ResourceTestBean(org.springframework.tests.sample.beans.ResourceTestBean) FatalBeanException(org.springframework.beans.FatalBeanException) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DependenciesBean(org.springframework.beans.testfixture.beans.DependenciesBean)

Aggregations

DependenciesBean (org.springframework.beans.testfixture.beans.DependenciesBean)17 Test (org.junit.jupiter.api.Test)16 ConstructorDependenciesBean (org.springframework.beans.factory.xml.ConstructorDependenciesBean)15 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)12 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)7 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)7 TestBean (org.springframework.beans.testfixture.beans.TestBean)7 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)6 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 FatalBeanException (org.springframework.beans.FatalBeanException)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)1 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)1