Search in sources :

Example 6 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method autowireWithSatisfiedJavaBeanDependency.

@Test
void autowireWithSatisfiedJavaBeanDependency() {
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("name", "Rod");
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setPropertyValues(pvs);
    lbf.registerBeanDefinition("rod", bd);
    assertThat(lbf.getBeanDefinitionCount()).isEqualTo(1);
    // Depends on age, name and spouse (TestBean)
    Object registered = lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
    assertThat(lbf.getBeanDefinitionCount()).isEqualTo(1);
    DependenciesBean kerry = (DependenciesBean) registered;
    TestBean rod = (TestBean) lbf.getBean("rod");
    assertThat(kerry.getSpouse()).isSameAs(rod);
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.beans.testfixture.beans.DependenciesBean) Test(org.junit.jupiter.api.Test)

Example 7 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method autowireExistingBeanByTypeWithDependencyCheck.

@Test
void autowireExistingBeanByTypeWithDependencyCheck() {
    DependenciesBean existingBean = new DependenciesBean();
    assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true));
}
Also used : ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.beans.testfixture.beans.DependenciesBean) Test(org.junit.jupiter.api.Test)

Example 8 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method autowireExistingBeanByName.

@Test
void autowireExistingBeanByName() {
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spouse", bd);
    DependenciesBean existingBean = new DependenciesBean();
    lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);
    TestBean spouse = (TestBean) lbf.getBean("spouse");
    assertThat(spouse).isEqualTo(existingBean.getSpouse());
    assertThat(BeanFactoryUtils.beanOfType(lbf, TestBean.class)).isSameAs(spouse);
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.beans.testfixture.beans.DependenciesBean) Test(org.junit.jupiter.api.Test)

Example 9 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method autowireExistingBeanByTypeWithNoDependencyCheck.

@Test
void autowireExistingBeanByTypeWithNoDependencyCheck() {
    DependenciesBean existingBean = new DependenciesBean();
    lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    assertThat(existingBean.getSpouse()).isNull();
}
Also used : ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.beans.testfixture.beans.DependenciesBean) Test(org.junit.jupiter.api.Test)

Example 10 with DependenciesBean

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

the class DefaultListableBeanFactoryTests method autowireBeanByNameWithNoDependencyCheck.

@Test
void autowireBeanByNameWithNoDependencyCheck() {
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spous", bd);
    DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    assertThat(bean.getSpouse()).isNull();
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.beans.testfixture.beans.DependenciesBean) Test(org.junit.jupiter.api.Test)

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