title: Failed to load applicationContext: NoSuchBeanDefinitionException when running JUnit test in Spring MVC app
body: <p>When I run:</p>
  
  <pre><code>package se.hsr.web;
  
  import java.util.List;
  
  import junit.framework.Assert;
  
  import se.hsr.web.UserDao;
  import se.hsr.web.User;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration( locations = { "classpath:HSR-servlet.xml" })
  public class UserTest {
   private UserDao dao;
  
   @Autowired
   public void setDao(UserDao dao) {
    this.dao = dao;
   }
  
   @Test
   public void testCreateData() {
    int expectedResult = 1;
    User user = new User();
    user.setAge(23);
    user.setUserName("Adit");
    user.setRegistered(true);
    dao.saveUser(user);
    Assert.assertEquals(expectedResult, dao.getAllUser(new User()).size());
   }
  
   @Test
   public void testRetrieveData() {
    List&lt;User&gt; userList = dao.getAllUser(new User());
    Assert.assertEquals(1, userList.size());
    User userExpected = userList.get(0);
    User userResult = dao.selectUserById(userExpected.getUserId());
    Assert.assertEquals(userExpected.getUserId(), userResult.getUserId());
   }
  
   @Test
   public void testUpdateData() {
    List&lt;User&gt; userList = dao.getAllUser(new User());
    Assert.assertEquals(1, userList.size());
    User userExpected = userList.get(0);
    userExpected.setUserName("Singgih");
    dao.saveUser(userExpected);
    User userResult = dao.selectUserById(userExpected.getUserId());
    Assert.assertEquals(userExpected.getUserName(), userResult
      .getUserName());
   }
  
   @Test
   public void testDeleteData() {
    List&lt;User&gt; userList = dao.getAllUser(new User());
    Assert.assertEquals(1, userList.size());
    User userExpected = userList.get(0);
    dao.deleteUser(userExpected);
    User userResult = dao.selectUserById(userExpected.getUserId());
    Assert.assertEquals(userResult, null);
   }
  }
  </code></pre>
  
  <p>The console output is: </p>
  
  <pre><code>... 24 more
      Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void se.hsr.web.UserDaoImpl.setSessionFactory(org.hibernate.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: {}
       at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:589)
       at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
       at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
       ... 38 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: {}
       at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
       at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
       at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
       at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:547)
       ... 40 more
  </code></pre>
  
  <p>My HSR-servlet.xml, db-config.xml and configuration.properties all rest in the src/main/resources folder.</p>
  
  <p>HSR-servlet.xml:</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:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;context:component-scan
          base-package="se.hsr.web"/&gt;
  
      &lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
          &lt;property name="viewClass"
              value="org.springframework.web.servlet.view.JstlView" /&gt;
          &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
      &lt;bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
    &lt;property name="locations"&gt;
     &lt;list&gt;
      &lt;value&gt;/configuration.properties&lt;/value&gt;
     &lt;/list&gt;
    &lt;/property&gt;
   &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>db-config.xml:</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:p="http://www.springframework.org/schema/p"
   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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
   "&gt;
   &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close"&gt;
  
    &lt;property name="driverClass"&gt;
     &lt;value&gt;${jdbc.driver.className}&lt;/value&gt;
    &lt;/property&gt;
    &lt;property name="jdbcUrl"&gt;
     &lt;value&gt;${jdbc.url}&lt;/value&gt;
    &lt;/property&gt;
    &lt;property name="user"&gt;
     &lt;value&gt;${jdbc.username}&lt;/value&gt;
    &lt;/property&gt;
    &lt;property name="password"&gt;
     &lt;value&gt;${jdbc.password}&lt;/value&gt;
    &lt;/property&gt;
   &lt;/bean&gt;
   &lt;bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
    &lt;property name="dataSource"&gt;
     &lt;ref bean="dataSource" /&gt;
    &lt;/property&gt;
    &lt;property name="packagesToScan" value="se.hsr.web" /&gt;
    &lt;property name="hibernateProperties"&gt;
     &lt;props&gt;
      &lt;prop key="hibernate.dialect"&gt;${jdbc.hibernate.dialect}&lt;/prop&gt;
      &lt;prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/prop&gt;
      &lt;!-- uncomment this for first time run--&gt;
      &lt;prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/prop&gt;
      &lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt;
     &lt;/props&gt;
    &lt;/property&gt;
   &lt;/bean&gt;
   &lt;bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
    &lt;property name="sessionFactory"&gt;
     &lt;ref bean="sessionFactory" /&gt;
    &lt;/property&gt;
   &lt;/bean&gt;
   &lt;tx:annotation-driven /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>What could the problem be? Thankful for any help.</p>
  
  <p>UPDATE:</p>
  
  <p>new errormessage after changing the code after the tip from the first commenter.</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.mchange.v2.c3p0.ComboPooledDataSource] for bean with name 'dataSource' defined in class path resource [db-config.xml]; nested exception is java.lang.ClassNotFoundException: com.mchange.v2.c3p0.ComboPooledDataSource
      at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1250)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:433)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
      ... 80 more
  Caused by: java.lang.ClassNotFoundException: com.mchange.v2.c3p0.ComboPooledDataSource
      at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
      at org.springframework.util.ClassUtils.forName(ClassUtils.java:257)
      at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:408)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1271)
      at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1242)
      ... 86 more
  </code></pre>