title: NoUniqueBeanDefinitionException while testing dao test class in spring
body: <p>Hi i am trying to test my DaoTest class as a junit test.While running the class I get:</p>
  
  <blockquote>
    <p>java.lang.NoClassDefFoundError:
    org/springframework/beans/factory/NoUniqueBeanDefinitionException.</p>
  </blockquote>
  
  <p>I looked upon the internet and found out that this might be due to more than onre reference to your beans in the configuration file. But i am not able to figure out how do i have more than one reference to the beans. </p>
  
  <p>Here is <code>UserDaoTest</code> class:</p>
  
  <pre><code>@ContextConfiguration (locations = "classpath:persistence-beans.xml")
  public class UserDaoTest extends DomainAwareBase {
  
  @Autowired
  @Qualifier("userDao")
  private UserDao userDao;
  
  @Test
  public void testUpdate() {
  
      int size = userDao.list().size();
  
      User user = new User(1, "jimmy", "password", "admin", "sales");
  
      userDao.add(user);
  
      Assert.assertTrue(size &lt; userDao.list().size());
  
  }
  
  @Test
  public void testFind() {
      User user = new User(1, "jimmy", "password", "admin", "sales");
  
      userDao.add(user);
      User found = userDao.find(user.getUserId());
  
      Assert.assertEquals(user, found);
  }
  
  @Test
  public void testList() {
      Assert.assertEquals(0, userDao.list().size());
      User user1 = new User(1, "jimmy", "password", "admin", "Sales");
      User user2 = new User(2, "pankit", "pankit", "admin", "HR");
      User user3 = new User(3, "abc", "abc", "admin", "Engineer");
  
      List&lt;User&gt; users = Arrays.asList(user1, user2, user3);
  
      for (User user : users) {
  
          userDao.add(user);
      }
  
      List&lt;User&gt; found = userDao.list();
      Assert.assertEquals(3, userDao.list().size());
  
      for (User user : found) {
  
          Assert.assertTrue(users.contains(user));
  
      }
  
  }
  
  @Test
  public void testRemove() {
  
      User user1 = new User(1, "jimmy", "password", "admin", "Sales");
  
      userDao.add(user1);
      Assert.assertEquals(user1, userDao.find(user1.getUserId()));
  
      userDao.remove(user1);
      Assert.assertNull(userDao.find(user1.getUserId()));
  
  
  }
  
  }
  </code></pre>
  
  <p>and this is my <code>persistence-beans.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"&gt;
  
  
  &lt;!-- we can use annotations --&gt;
  &lt;context:annotation-config /&gt;
  
  &lt;!-- package to look for annotated classes --&gt;
  &lt;context:component-scan base-package="com.finalproject" /&gt;
  
  &lt;!-- we will manage transactions with annotations --&gt;
  &lt;tx:annotation-driven /&gt;
  
  &lt;!-- data source for our database --&gt;
  
  &lt;bean id="dataSource"
  
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
  
      &lt;property name="driverClassName"
          value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" /&gt;
  
      &lt;property name="url"
          value="jdbc:mysql://localhost:3306/leadmanagement_db" /&gt;
      &lt;property name="username" value="root" /&gt;
  
      &lt;property name="password" value="root" /&gt;
  
  &lt;/bean&gt;
  
  &lt;bean id="transactionManager"
  
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
  
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  
  &lt;/bean&gt;
  
  &lt;!-- configure hibernate session factory --&gt;
  
  &lt;bean id="sessionFactory"
  
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
  
      &lt;property name="dataSource" ref="dataSource" /&gt;
  
      &lt;property name="configLocation"&gt;
  
  
          &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt;
  
  
      &lt;/property&gt;
  
      &lt;property name="hibernateProperties"&gt;
  
          &lt;props&gt;
  
              &lt;prop key="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
  
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
  
              &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
  
          &lt;/props&gt;
  
      &lt;/property&gt;
  
  &lt;/bean&gt;
  </code></pre>
  
  <p>
  <strong>EDIT</strong>
  This is my stack trace</p>
  
  <pre><code>java.lang.NoClassDefFoundError: org/springframework/beans/factory/NoUniqueBeanDefinitionException
  at java.lang.Class.getDeclaredMethods0(Native Method)
  at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
  at java.lang.Class.getDeclaredMethods(Class.java:1808)
  at org.springframework.core.type.StandardAnnotationMetadata.hasAnnotatedMethods(StandardAnnotationMetadata.java:159)
  at org.springframework.context.annotation.ConfigurationClassUtils.isLiteConfigurationCandidate(ConfigurationClassUtils.java:106)
  at org.springframework.context.annotation.ConfigurationClassUtils.checkConfigurationClassCandidate(ConfigurationClassUtils.java:87)
  at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:233)
  at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:203)
  at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:617)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446)
  at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:120)
  at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
  at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
  at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
  at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
  at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
  at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
  at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
  at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
  at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
  at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  </code></pre>
  
  <p>Caused by: java.lang.ClassNotFoundException: org.springframework.beans.factory.NoUniqueBeanDefinitionException
      at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
      ... 41 more</p>