Announcement Announcement Module
Collapse
No announcement yet.
Trouble wiring datasource in a JUnit test Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trouble wiring datasource in a JUnit test

    Hi,

    I'm using Spring 3.1.0.M3. I have this in my JUnit test ...

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(loader=AnnotationConfigContextLoader.class, 
    		classes={DataSource.class, WebLeadsDAOImpl.class})
    public class TestDB {
    
    	@Autowired
    	private SessionFactory sessionFactory;
    which results in an exception ...

    Code:
            ... 32 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [NnaSessionFactory, NcvSessionFactory]
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:796)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:476)
            ... 34 more
    so I tried using a @Qualifier to pick one of the two bean names ...

    Code:
    	@Qualifier("NnaSessionFactory")
    	private SessionFactory sessionFactory;
    but then my sessionFactory is null. So how do I properly wire my session factory in my JUnit test? - Dave

  • #2
    For starters your configuration is at least weird.. I cannot imagine that javax.sql.DataSource and your dao are annotated with @Configuration (which is what you are telling the context loading stuff).

    Only @Qualifier is going to nothing... You have to use both @Autowired and @Qualifier...

    Comment


    • #3
      Adding @Autowired fixed the problem. Thanks, - Dave

      Comment

      Working...
      X