use of org.springframework.beans.testfixture.beans.DependenciesBean in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method registerExistingSingletonWithAutowire.
@Test
void registerExistingSingletonWithAutowire() {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("name", "Tony");
pvs.add("age", "48");
RootBeanDefinition bd = new RootBeanDefinition(DependenciesBean.class);
bd.setPropertyValues(pvs);
bd.setDependencyCheck(RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
lbf.registerBeanDefinition("test", bd);
Object singletonObject = new TestBean();
lbf.registerSingleton("singletonObject", singletonObject);
assertThat(lbf.containsBean("singletonObject")).isTrue();
assertThat(lbf.isSingleton("singletonObject")).isTrue();
assertThat(lbf.getType("singletonObject")).isEqualTo(TestBean.class);
assertThat(lbf.getAliases("singletonObject").length).isEqualTo(0);
DependenciesBean test = (DependenciesBean) lbf.getBean("test");
assertThat(lbf.getBean("singletonObject")).isEqualTo(singletonObject);
assertThat(test.getSpouse()).isEqualTo(singletonObject);
}
use of org.springframework.beans.testfixture.beans.DependenciesBean in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method autowireExistingBeanByNameWithNoDependencyCheck.
@Test
void autowireExistingBeanByNameWithNoDependencyCheck() {
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
lbf.registerBeanDefinition("spous", bd);
DependenciesBean existingBean = new DependenciesBean();
lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
assertThat(existingBean.getSpouse()).isNull();
}
use of org.springframework.beans.testfixture.beans.DependenciesBean in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method autowireBeanByTypeWithTwoMatchesAndPriority.
@Test
void autowireBeanByTypeWithTwoMatchesAndPriority() {
lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
RootBeanDefinition bd = new RootBeanDefinition(HighPriorityTestBean.class);
RootBeanDefinition bd2 = new RootBeanDefinition(LowPriorityTestBean.class);
lbf.registerBeanDefinition("test", bd);
lbf.registerBeanDefinition("spouse", bd2);
DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
assertThat(bean.getSpouse()).isEqualTo(lbf.getBean("test"));
}
use of org.springframework.beans.testfixture.beans.DependenciesBean in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method autowireBeanByTypePrimaryTakesPrecedenceOverPriority.
@Test
void autowireBeanByTypePrimaryTakesPrecedenceOverPriority() {
lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
RootBeanDefinition bd = new RootBeanDefinition(HighPriorityTestBean.class);
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
bd2.setPrimary(true);
lbf.registerBeanDefinition("test", bd);
lbf.registerBeanDefinition("spouse", bd2);
DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
assertThat(bean.getSpouse()).isEqualTo(lbf.getBean("spouse"));
}
use of org.springframework.beans.testfixture.beans.DependenciesBean in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method autowireExistingBeanByNameWithDependencyCheck.
@Test
void autowireExistingBeanByNameWithDependencyCheck() {
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
lbf.registerBeanDefinition("spous", bd);
DependenciesBean existingBean = new DependenciesBean();
assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true));
}
Aggregations