Search in sources :

Example 6 with UnsatisfiedDependencyException

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();
}
Also used : UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 7 with UnsatisfiedDependencyException

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();
}
Also used : UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 8 with UnsatisfiedDependencyException

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());
    }
}
Also used : UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 9 with UnsatisfiedDependencyException

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());
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Example 10 with UnsatisfiedDependencyException

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());
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Aggregations

UnsatisfiedDependencyException (org.springframework.beans.factory.UnsatisfiedDependencyException)24 Test (org.junit.Test)17 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)9 BeanCreationException (org.springframework.beans.factory.BeanCreationException)8 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)8 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)8 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)7 ITestBean (org.springframework.tests.sample.beans.ITestBean)5 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)5 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)5 TestBean (org.springframework.tests.sample.beans.TestBean)5 LinkedHashSet (java.util.LinkedHashSet)4 BeansException (org.springframework.beans.BeansException)4 TypeMismatchException (org.springframework.beans.TypeMismatchException)4 InjectionPoint (org.springframework.beans.factory.InjectionPoint)4 TypeConverter (org.springframework.beans.TypeConverter)3 MethodParameter (org.springframework.core.MethodParameter)3 PropertyDescriptor (java.beans.PropertyDescriptor)2 Constructor (java.lang.reflect.Constructor)2 PrivilegedAction (java.security.PrivilegedAction)2