title: SessionFactory not getting autowired while trying to Unit test a DAO class
body: <p>I'm stuck up in a weird situation in which a sessionFactory I've defined is getting autowired to a DAO class when i run it as a web app.</p>
  
  <p>However, when i try to unit test this DAO class, spring is not autowiring the sessionFactory to the DAO class and throwing the below exception.</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'activityDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.adwitiya.vdb.data.activity.ActivityDAO.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)   at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)  at org.springframework.context.annotation.AnnotationConfigApplicationContext.&lt;init&gt;(AnnotationConfigApplicationContext.java:84)     at com.adwitiya.vdb.data.activity.ActivityDAOTest.testConstructor(ActivityDAOTest.java:31)  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:601)     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)   at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)    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.junit.runners.ParentRunner.run(ParentRunner.java:300)    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)  at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:601)     at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)   at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)  at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)  at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.adwitiya.vdb.data.activity.ActivityDAO.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}   at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)   ... 41 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}  at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:952)     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:821)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735)  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)    ... 43 more
  </code></pre>
  
  <p>I've done hell lot of googling yet couldn't find a solution for this problem. I'd be really glad and grateful if you can help me out.</p>
  
  <p>Here's my configuration and class files.</p>
  
  <pre><code>Web.xml
  -------
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0"&gt;
      &lt;session-config&gt;
          &lt;session-timeout&gt;
              30
          &lt;/session-timeout&gt;
      &lt;/session-config&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;
              /WEB-INF/vdb-data.xml
          &lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;filter&gt;
          &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;
              org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
          &lt;/filter-class&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;vdb&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;vdb&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>My primary MVC Config file is below.</strong></p>
  
  <pre><code>vdb-servlet.xml
  ---------------
  &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:aop="http://www.springframework.org/schema/aop"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:jee="http://www.springframework.org/schema/jee"
         xmlns:lang="http://www.springframework.org/schema/lang"
         xmlns:p="http://www.springframework.org/schema/p"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:util="http://www.springframework.org/schema/util"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.1.xsd
          http://www.springframework.org/schema/jee
          http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
          http://www.springframework.org/schema/lang
          http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
          http://www.springframework.org/schema/util
          http://www.springframework.org/schema/util/spring-util-3.1.xsd"&gt;
  
      &lt;mvc:resources mapping="/skin/**" location="/skin/" /&gt;
  
      &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="com.adwitiya.vdb" /&gt;
  
      &lt;bean id="jspViewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="viewClass"
                    value="org.springframework.web.servlet.view.JstlView" /&gt;
          &lt;property name="prefix" value="/WEB-INF/view/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
  
      &lt;mvc:annotation-driven /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>Below is the database related spring configuration file</strong></p>
  
  <pre><code>vdb-data.xml
  ------------
  &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:aop="http://www.springframework.org/schema/aop"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:jee="http://www.springframework.org/schema/jee"
         xmlns:lang="http://www.springframework.org/schema/lang"
         xmlns:p="http://www.springframework.org/schema/p"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:util="http://www.springframework.org/schema/util"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.1.xsd
          http://www.springframework.org/schema/jee
          http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
          http://www.springframework.org/schema/lang
          http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
          http://www.springframework.org/schema/util
          http://www.springframework.org/schema/util/spring-util-3.1.xsd"&gt;
  
      &lt;bean id="messageSource"
            class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
          &lt;property name="basename" value="classpath:messages" /&gt;
          &lt;property name="defaultEncoding" value="UTF-8" /&gt;
      &lt;/bean&gt;
  
      &lt;context:property-placeholder location="classpath:database.properties" /&gt;
  
      &lt;tx:annotation-driven transaction-manager="hibernateTransactionManager"/&gt;
  
      &lt;bean id="dataSource"
                class="org.springframework.jdbc.datasource.DriverManagerDataSource"&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.adwitiya.vdb.domain" /&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.c3p0.min_size"&gt;${hibernate.c3p0.min_size}&lt;/prop&gt;
                  &lt;prop key="hibernate.c3p0.max_size"&gt;${hibernate.c3p0.max_size}&lt;/prop&gt;
                  &lt;prop key="hibernate.c3p0.timeout"&gt;${hibernate.c3p0.timeout}&lt;/prop&gt;
                  &lt;prop key="hibernate.c3p0.max_statements"&gt;${hibernate.c3p0.max_statements}&lt;/prop&gt;
                  &lt;prop key="hibernate.c3p0.idle_test_period"&gt;${hibernate.c3p0.idle_test_period}&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>The DAO Class</strong></p>
  
  <pre><code>ActivityDAO
  -----------
  package com.adwitiya.vdb.data.activity;
  
  import com.adwitiya.vdb.domain.Activity;
  import org.apache.log4j.Logger;
  import org.hibernate.HibernateException;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Repository;
  
  @Repository("activityDAO")
  public class ActivityDAO
  {
      @Autowired
      private SessionFactory sessionFactory;
      private Session session;
      private boolean status;
      private static Logger logger = Logger.getLogger(ActivityDAO.class);
  
      public ActivityDAO()
      {
      try
      {
          session = sessionFactory.getCurrentSession();
      }catch(Exception exception)
      {
          logger.error(("Unable to initiate session because of the problem " + exception.getMessage()));
      }
      }
  
      public boolean add(Activity activity)
      {
      try
      {
          logger.info("Trying to add an activity " + activity.toString());
          session.save(activity);
          status = true;
          logger.info("Added the activity " + activity.toString() + " successfully");
      }catch(HibernateException exception)
      {
          logger.error("Couldn't add an activity " + activity.toString() + " because of the problem " + exception.getMessage());
      }catch(Exception exception)
      {
          logger.error("Couldn't add an activity " + activity.toString() + " because of the problem " + exception.getMessage());
      }
      return status;
      }
  
      public boolean modify(Activity activity)
      {
      try
      {
          logger.info("Trying to modify the activity " + activity.toString());
          session.saveOrUpdate(activity);
          status = true;
          logger.info("Updated the activity " + activity.toString() + "successfully");
      }catch(HibernateException exception)
      {
          logger.error("Couldn't modify the activity " + activity.toString() + " because of the problem " + exception.getMessage());
      }catch(Exception exception)
      {
          logger.error("Couldn't modify the activity " + activity.toString() + " because of the problem " + exception.getMessage());
      }
      return status;
      }
  
      public boolean delete(Activity activity)
      {
      try
      {
          logger.info("Trying to delete the activity " + activity.toString());
          session.delete(activity);
          status=true;
          logger.info("Deleted the activity " + activity.toString() + " successfully");
      }catch(HibernateException exception)
      {
          logger.error("Couldn't delete the activity " + activity.toString() + " because of the problem " + exception.getMessage());
      }catch(Exception exception)
      {
          logger.error("Couldn't delete the activity " + activity.toString() + " because of the problem " + exception.getMessage());
      }
      return status;
      }
  
      public SessionFactory getSessionFactory()
      {
      return sessionFactory;
      }
  
      public void setSessionFactory(SessionFactory sessionFactory)
      {
      this.sessionFactory = sessionFactory;
      }
  
      public Session getSession()
      {
      return session;
      }
  
      public void setSession(Session session)
      {
      this.session = session;
      }
  
      public boolean isStatus()
      {
      return status;
      }
  
      public void setStatus(boolean status)
      {
      this.status = status;
      }
  }
  </code></pre>
  
  <p><strong>And finally, the ActivityDAOTest class</strong></p>
  
  <pre><code>ActivityDAOTest
  ---------------
  
  package com.adwitiya.vdb.data.activity;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  import org.junit.Test;
  import static org.junit.Assert.assertEquals;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  import org.springframework.test.context.ContextConfiguration;
  
  @ContextConfiguration( locations = { "classpath:vdb-servlet.xml", "classpath:vdb-data.xml"})
  public class ActivityDAOTest
  {
      public ActivityDAOTest()
      {
      }
  
      @Test
      public void testConstructor()
      {
      Session thisSession = new Configuration().configure().buildSessionFactory().openSession();
      ApplicationContext appcontext = new AnnotationConfigApplicationContext("com.adwitiya.vdb");
      ActivityDAO activityDAO = (ActivityDAO) appcontext.getBean("ActivityDAO");
      Session session = activityDAO.getSession();
      assertEquals(thisSession,session);
      }
  }
  </code></pre>