title: JUnit/Spring/Hibernate - No qualifying bean of type [javax.sql.DataSource]
body: <blockquote>
    <p>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'JobSeekerServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests.setDataSource(javax.sql.DataSource); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:82)
        at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:199)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:251)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:253)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:216)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:82)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:60)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:67)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:162)
        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:459)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests.setDataSource(javax.sql.DataSource); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
        ... 26 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1261)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1009)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:600)
        ... 28 more</p>
  </blockquote>
  
  <h2> Test class</h2>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)  
  @ContextConfiguration(locations="classpath*:spring-config.xml") 
  @TransactionConfiguration(defaultRollback=true,transactionManager="txManager")
  @Transactional
  public class JobSeekerServiceTest extends AbstractTransactionalJUnit4SpringContextTests{
  
      private JobSeekerDao jobseekerDao = new JobSeekerDao();
  
      @Test
      public void testGetCandidateForEmail(){
          JobSeeker result = new JobSeeker();
          JobSeeker expected = new JobSeeker();
          expected.setName("Dummy Job seeker 1");expected.setEmail("email@yahoo.com");expected.setCity("City");expected.setPhone("04523122");
  
          result = jobseekerDao.getCandidateForEmail("email@yahoo.com");
          assertEquals(result, expected);
      }
  }
  </code></pre>
  
  <h2>JobSeekerDao</h2>
  
  <pre><code>@SuppressWarnings("unchecked")
      @Override
      public JobSeeker getCandidateForEmail(String email) {
          LOGGER.debug("Searching the candidate after an email:"+email);
          Session session = getCurrentSession();
  
          List&lt;JobSeeker&gt; candidates = session.createQuery("from JobSeeker where email=?")
                  .setParameter(0, email)
                  .list();
  
  
          if (candidates.size() &gt; 0) {
              return candidates.get(0);
          } else {
              return null;
          }
      }
  </code></pre>
  
  <h2>Spring-Config.xml</h2>
  
  <pre><code>&lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;  
              &lt;property name="driverClassName"    value="${database.driver}" /&gt;  
              &lt;property name="url"                value="${database.url}" /&gt;  
              &lt;property name="username"           value="${database.user}" /&gt;  
              &lt;property name="password"           value="${database.password}" /&gt;  
          &lt;/bean&gt;  
  
          &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;  
              &lt;property name="dataSource" ref="dataSource" /&gt;
              &lt;property name="packagesToScan" value="com.awinta.hrms"/&gt;
  
              &lt;property name="hibernateProperties"&gt;  
                  &lt;props&gt;  
                      &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;  
                      &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;
                      &lt;prop key="hibernate.format_sql"&gt;${hibernate.format_sql}&lt;/prop&gt;
                      &lt;prop key="hibernate.use_sql_comments"&gt;${hibernate.use_sql_comments}&lt;/prop&gt;
  
                      &lt;prop key="hibernate.hbm2ddl.auto"&gt;${hibernate.hbm2ddl.auto}&lt;/prop&gt;
  
                      &lt;prop key="hibernate.temp.use_jdbc_metadata_defaults"&gt;${hibernate.temp.use_jdbc_metadata_defaults}&lt;/prop&gt;
  
                      &lt;prop key="hibernate.connection.pool_size"&gt;${hibernate.connection.pool_size}&lt;/prop&gt;
                  &lt;/props&gt;  
              &lt;/property&gt;  
          &lt;/bean&gt; 
  
          &lt;bean id="txManager" 
              class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
              name="txManager"&gt;  
              &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
          &lt;/bean&gt; 
  </code></pre>
  
  <p>Any ideas why I am getting this error? I am newbie in Spring,Hibernate,JUnit. In conclusion I think I should do somehow like this. Or how should I do this test class? </p>
  
  <h1>UPDATE</h1>
  
  <p>I read somewhere cause the problem is extends AbstractTransactionalJUnit4SpringContextTests, and without it I don`t get anymore this error.</p>