use of org.springframework.beans.factory.UnsatisfiedDependencyException in project spring-framework by spring-projects.
the class AutowiredAnnotationBeanPostProcessorTests method testCustomAnnotationRequiredFieldResourceInjectionFailsWhenNoDependencyFound.
@Test
public void testCustomAnnotationRequiredFieldResourceInjectionFailsWhenNoDependencyFound() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setAutowiredAnnotationType(MyAutowired.class);
bpp.setRequiredParameterName("optional");
bpp.setRequiredParameterValue(false);
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("customBean", new RootBeanDefinition(CustomAnnotationRequiredFieldResourceInjectionBean.class));
try {
bf.getBean("customBean");
fail("Should have thrown UnsatisfiedDependencyException");
} catch (UnsatisfiedDependencyException ex) {
// expected
assertSame(CustomAnnotationRequiredFieldResourceInjectionBean.class, ex.getInjectionPoint().getField().getDeclaringClass());
}
bf.destroySingletons();
}
use of org.springframework.beans.factory.UnsatisfiedDependencyException in project spring-framework by spring-projects.
the class AutowiredAnnotationBeanPostProcessorTests method testCustomAnnotationRequiredMethodResourceInjectionFailsWhenMultipleDependenciesFound.
@Test
public void testCustomAnnotationRequiredMethodResourceInjectionFailsWhenMultipleDependenciesFound() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setAutowiredAnnotationType(MyAutowired.class);
bpp.setRequiredParameterName("optional");
bpp.setRequiredParameterValue(false);
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("customBean", new RootBeanDefinition(CustomAnnotationRequiredMethodResourceInjectionBean.class));
TestBean tb1 = new TestBean();
bf.registerSingleton("testBean1", tb1);
TestBean tb2 = new TestBean();
bf.registerSingleton("testBean2", tb2);
try {
bf.getBean("customBean");
fail("Should have thrown UnsatisfiedDependencyException");
} catch (UnsatisfiedDependencyException ex) {
// expected
assertSame(CustomAnnotationRequiredMethodResourceInjectionBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass());
}
bf.destroySingletons();
}
use of org.springframework.beans.factory.UnsatisfiedDependencyException in project spring-framework by spring-projects.
the class AutowiredAnnotationBeanPostProcessorTests method testConstructorResourceInjectionWithNoCandidatesAndNoFallback.
@Test
public void testConstructorResourceInjectionWithNoCandidatesAndNoFallback() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorWithoutFallbackBean.class));
try {
bf.getBean("annotatedBean");
fail("Should have thrown UnsatisfiedDependencyException");
} catch (UnsatisfiedDependencyException ex) {
// expected
assertSame(ConstructorWithoutFallbackBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass());
}
}
use of org.springframework.beans.factory.UnsatisfiedDependencyException in project spring-framework by spring-projects.
the class InjectAnnotationAutowireContextTests method testAutowiredConstructorArgumentWithSingleNonQualifiedCandidate.
@Test
public void testAutowiredConstructorArgumentWithSingleNonQualifiedCandidate() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs = new ConstructorArgumentValues();
cavs.addGenericArgumentValue(JUERGEN);
RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
context.registerBeanDefinition(JUERGEN, person);
context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
try {
context.refresh();
fail("expected BeanCreationException");
} catch (BeanCreationException e) {
assertTrue(e instanceof UnsatisfiedDependencyException);
assertEquals("autowired", e.getBeanName());
}
}
use of org.springframework.beans.factory.UnsatisfiedDependencyException in project spring-framework by spring-projects.
the class QualifierAnnotationAutowireContextTests method autowiredConstructorArgumentWithSingleNonQualifiedCandidate.
@Test
public void autowiredConstructorArgumentWithSingleNonQualifiedCandidate() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs = new ConstructorArgumentValues();
cavs.addGenericArgumentValue(JUERGEN);
RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
context.registerBeanDefinition(JUERGEN, person);
context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
try {
context.refresh();
fail("expected BeanCreationException");
} catch (BeanCreationException e) {
assertTrue(e instanceof UnsatisfiedDependencyException);
assertEquals("autowired", e.getBeanName());
}
}
Aggregations