Example usage for org.springframework.beans.factory UnsatisfiedDependencyException getCause

List of usage examples for org.springframework.beans.factory UnsatisfiedDependencyException getCause

Introduction

In this page you can find the example usage for org.springframework.beans.factory UnsatisfiedDependencyException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testAutowireBeanByTypeWithTwoPrimaryCandidates() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setPrimary(true);//  w ww. ja  v a2 s .c  om
    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());
    }
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testAutowireBeanByTypeWithIdenticalPriorityCandidates() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
    RootBeanDefinition bd = new RootBeanDefinition(HighPriorityTestBean.class);
    RootBeanDefinition bd2 = new RootBeanDefinition(HighPriorityTestBean.class);
    lbf.registerBeanDefinition("test", bd);
    lbf.registerBeanDefinition("spouse", bd2);

    try {//w  w w.ja  va  2 s . c  o m
        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());
        assertTrue(ex.getMessage().contains("5")); // conflicting priority
    }
}