Improving test coverage of determinePrimaryCandidate
Added tests checking the behaviour of autowiring by type when the @Primary annotation is present on the class. Fixed also some inconsistencies in the code.
This commit is contained in:
parent
3c28301ded
commit
48221798f5
|
|
@ -1045,11 +1045,11 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
if (primaryBeanName != null) {
|
if (primaryBeanName != null) {
|
||||||
boolean candidateLocal = containsBeanDefinition(candidateBeanName);
|
boolean candidateLocal = containsBeanDefinition(candidateBeanName);
|
||||||
boolean primaryLocal = containsBeanDefinition(primaryBeanName);
|
boolean primaryLocal = containsBeanDefinition(primaryBeanName);
|
||||||
if (candidateLocal == primaryLocal) {
|
if (candidateLocal && primaryLocal) {
|
||||||
throw new NoUniqueBeanDefinitionException(descriptor.getDependencyType(), candidateBeans.size(),
|
throw new NoUniqueBeanDefinitionException(descriptor.getDependencyType(), candidateBeans.size(),
|
||||||
"more than one 'primary' bean found among candidates: " + candidateBeans.keySet());
|
"more than one 'primary' bean found among candidates: " + candidateBeans.keySet());
|
||||||
}
|
}
|
||||||
else if (candidateLocal && !primaryLocal) {
|
else if (candidateLocal) {
|
||||||
primaryBeanName = candidateBeanName;
|
primaryBeanName = candidateBeanName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1511,6 +1511,41 @@ public class DefaultListableBeanFactoryTests {
|
||||||
assertNull(bean.getSpouse());
|
assertNull(bean.getSpouse());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAutowireBeanByTypeWithTwoMatchesAndOnePrimary() {
|
||||||
|
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||||
|
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
|
||||||
|
bd.setPrimary(true);
|
||||||
|
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
|
||||||
|
lbf.registerBeanDefinition("test", bd);
|
||||||
|
lbf.registerBeanDefinition("spouse", bd2);
|
||||||
|
|
||||||
|
DependenciesBean bean = (DependenciesBean)
|
||||||
|
lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
|
||||||
|
assertThat(bean.getSpouse(), equalTo(lbf.getBean("test")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAutowireBeanByTypeWithTwoPrimaryCandidates() {
|
||||||
|
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||||
|
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
|
||||||
|
bd.setPrimary(true);
|
||||||
|
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
|
||||||
|
bd2.setPrimary(true);
|
||||||
|
lbf.registerBeanDefinition("test", bd);
|
||||||
|
lbf.registerBeanDefinition("spouse", bd2);
|
||||||
|
|
||||||
|
try {
|
||||||
|
lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
|
||||||
|
fail("Should have thrown UnsatisfiedDependencyException");
|
||||||
|
}
|
||||||
|
catch (UnsatisfiedDependencyException ex) {
|
||||||
|
// expected
|
||||||
|
assertNotNull("Exception should have cause", ex.getCause());
|
||||||
|
assertEquals("Wrong cause type", NoUniqueBeanDefinitionException.class, ex.getCause().getClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAutowireExistingBeanByName() {
|
public void testAutowireExistingBeanByName() {
|
||||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue