Search in sources :

Example 21 with UnsatisfiedDependencyException

use of org.springframework.beans.factory.UnsatisfiedDependencyException in project spring-framework by spring-projects.

the class QualifierAnnotationAutowireContextTests method autowiredConstructorArgumentWithMultipleNonQualifiedCandidates.

@Test
public void autowiredConstructorArgumentWithMultipleNonQualifiedCandidates() {
    GenericApplicationContext context = new GenericApplicationContext();
    ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
    cavs1.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
    ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
    cavs2.addGenericArgumentValue(MARK);
    RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
    context.registerBeanDefinition(JUERGEN, person1);
    context.registerBeanDefinition(MARK, person2);
    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 22 with UnsatisfiedDependencyException

use of org.springframework.beans.factory.UnsatisfiedDependencyException in project spring-framework by spring-projects.

the class QualifierAnnotationAutowireContextTests method autowiredFieldDoesNotResolveWithBaseQualifierAndNonDefaultValueAndMultipleMatchingCandidates.

@Test
public void autowiredFieldDoesNotResolveWithBaseQualifierAndNonDefaultValueAndMultipleMatchingCandidates() {
    GenericApplicationContext context = new GenericApplicationContext();
    ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
    cavs1.addGenericArgumentValue("the real juergen");
    RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
    person1.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
    ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
    cavs2.addGenericArgumentValue("juergen imposter");
    RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
    person2.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
    context.registerBeanDefinition("juergen1", person1);
    context.registerBeanDefinition("juergen2", person2);
    context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedConstructorArgumentWithBaseQualifierNonDefaultValueTestBean.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) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Example 23 with UnsatisfiedDependencyException

use of org.springframework.beans.factory.UnsatisfiedDependencyException in project spring-framework by spring-projects.

the class EnableAsyncTests method properExceptionForResolvedProxyDependencyMismatch.

@Test
public void properExceptionForResolvedProxyDependencyMismatch() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(AsyncConfig.class, AsyncBeanUser.class, AsyncBeanWithInterface.class);
    try {
        ctx.refresh();
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException ex) {
        ex.printStackTrace();
        assertTrue(ex.getCause() instanceof BeanNotOfRequiredTypeException);
    }
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) BeanNotOfRequiredTypeException(org.springframework.beans.factory.BeanNotOfRequiredTypeException) Test(org.junit.Test)

Example 24 with UnsatisfiedDependencyException

use of org.springframework.beans.factory.UnsatisfiedDependencyException in project spring-framework by spring-projects.

the class EnableAsyncTests method properExceptionForExistingProxyDependencyMismatch.

@Test
public void properExceptionForExistingProxyDependencyMismatch() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(AsyncConfig.class, AsyncBeanWithInterface.class, AsyncBeanUser.class);
    try {
        ctx.refresh();
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException ex) {
        ex.printStackTrace();
        assertTrue(ex.getCause() instanceof BeanNotOfRequiredTypeException);
    }
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) BeanNotOfRequiredTypeException(org.springframework.beans.factory.BeanNotOfRequiredTypeException) 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