title: Spring, Failed to load ApplicationContext in tests
body: <p>I have following web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app
          version="3.0"
          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
          "&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;listener&gt;
          &lt;listener-class&gt;org.springframework.security.web.session.HttpSessionEventPublisher&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/applicationContext.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
      ...
  
      &lt;!-- Spring MVC --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;WEB-INF/webContext.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Thus from web.xml I load two configuration files:</p>
  
  <p><code>applicationContext.xml</code> and <code>webContext.xml</code></p>
  
  <p>I copied these files to <code>resources/META-INF</code>  and  write integration test with following signature:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = {"classpath:META-INF/applicationContext.xml", "classpath:META-INF/webContext.xml"})
  public class OwnerTerminalsControllerTest {
  </code></pre>
  
  <p>when I try to execute any method from test I see error like this:</p>
  
  <pre><code>     org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'conversionService' defined in class path resource [META-INF/webContext.xml]: Cannot resolve reference to bean 'terminalGroupFormatter' while setting bean property 'formatters' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'terminalGroupFormatter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.terminal.dao.TerminalGroupDao com.terminal.formatter.TerminalGroupFormatter.terminalGroupDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'terminalGroupDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.terminal.dao.impl.TerminalGroupDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying 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.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
  
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying 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:1301)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
      ... 70 more
  24.09.2015 12:28:49 ERROR: org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@7181ae3f] to prepare test instance [com.terminal.controller.owner.OwnerTerminalsControllerTest@6bc248ed]
  java.lang.IllegalStateException: Failed to load ApplicationContext
      at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
      .....
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying 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:1301)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
      ... 70 more
  </code></pre>
  
  <p>For me two configurations looks same and I have not ideas why from test context cannon be loaded</p>
  
  <h2>P.S.</h2>
  
  <p>inside <code>applicatioContext</code> I have following lines:</p>
  
  <pre><code>&lt;import resource="classpath:META-INF/dataContext.xml"/&gt;
  &lt;import resource="classpath:META-INF/restTemplateContext.xml" /&gt;
  &lt;import resource="classpath:META-INF/securityContext.xml"/&gt;
  </code></pre>
  
  <p>dataContext:</p>
  
  <pre><code>&lt;beans
          xmlns="http://www.springframework.org/schema/beans"
          xmlns:p="http://www.springframework.org/schema/p"
          xmlns:context="http://www.springframework.org/schema/context"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:jdbc="http://www.springframework.org/schema/jdbc"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:task="http://www.springframework.org/schema/task"
          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.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/jdbc
          http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  
           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
      &lt;context:component-scan base-package="com.terminal.domain, com.terminal.dao, com.terminal.utils"/&gt;
  
      &lt;bean id="transactionManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="messageSource"
            class="org.springframework.context.support.ResourceBundleMessageSource"&gt;
          &lt;property name="basenames"&gt;
              &lt;list&gt;
                  &lt;value&gt;mymessages&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;task:scheduler id="jobScheduler" pool-size="10"/&gt;
  
      &lt;beans profile="test"&gt;
  
          &lt;bean id="wrappedDataSource" class="net.bull.javamelody.SpringDataSourceFactoryBean"&gt;
              &lt;property name="targetName" value="dataSource" /&gt;
          &lt;/bean&gt;
  
          &lt;bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource"&gt;
              &lt;property name="driverClassName" value="org.h2.Driver" /&gt;
              &lt;property name="url" value="jdbc:h2:~/test;MODE=PostgreSQL" /&gt;
              &lt;property name="username" value="sa" /&gt;
              &lt;property name="password" value="" /&gt;
          &lt;/bean&gt;
  
          &lt;bean id="sessionFactory"
                class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
              &lt;property name="dataSource" ref="wrappedDataSource"/&gt;
              &lt;property name="configLocation"&gt;
                  &lt;value&gt;classpath:hibernate-test.cfg.xml&lt;/value&gt;
              &lt;/property&gt;
              &lt;property name="hibernateProperties"&gt;
                  &lt;props&gt;
                      &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                      &lt;prop key="hibernate.connection.charSet"&gt;UTF-8&lt;/prop&gt;
                      &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                      &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
                      &lt;prop key="hbm2ddl.auto"&gt;create-drop&lt;/prop&gt;
                  &lt;/props&gt;
              &lt;/property&gt;
          &lt;/bean&gt;
  
          &lt;context:property-placeholder location="classpath:jdbc.properties"/&gt;
  
      &lt;/beans&gt;
      &lt;beans profile="dev"&gt;
  
          &lt;bean id="wrappedDataSource" class="net.bull.javamelody.SpringDataSourceFactoryBean"&gt;
              &lt;property name="targetName" value="dataSource" /&gt;
          &lt;/bean&gt;
  
          &lt;bean id="sessionFactory"
                class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
              &lt;property name="dataSource" ref="wrappedDataSource"/&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="hibernate.show_sql"&gt;true&lt;/prop&gt;
                      &lt;prop key="hibernate.dialect"&gt;${jdbc.dialect}&lt;/prop&gt;
                      &lt;prop key="hibernate.connection.charSet"&gt;UTF-8&lt;/prop&gt;
                      &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                      &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
                      &lt;prop key="hbm2ddl.auto"&gt;validate&lt;/prop&gt;
                  &lt;/props&gt;
              &lt;/property&gt;
          &lt;/bean&gt;
  
          &lt;context:property-placeholder location="classpath:jdbc-local.properties"/&gt;
          &lt;bean id="dataSource"
                class="org.springframework.jdbc.datasource.DriverManagerDataSource"
                p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}"
                p:username="${jdbc.username}" p:password="${jdbc.password}"/&gt;
      &lt;/beans&gt;
      &lt;beans profile="prod"&gt;
  
          &lt;bean id="wrappedDataSource" class="net.bull.javamelody.SpringDataSourceFactoryBean"&gt;
              &lt;property name="targetName" value="dataSource" /&gt;
          &lt;/bean&gt;
  
          &lt;bean id="sessionFactory"
                class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
              &lt;property name="dataSource" ref="wrappedDataSource"/&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="hibernate.show_sql"&gt;true&lt;/prop&gt;
                      &lt;prop key="hibernate.dialect"&gt;${jdbc.dialect}&lt;/prop&gt;
                      &lt;prop key="hibernate.connection.charSet"&gt;UTF-8&lt;/prop&gt;
                      &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                      &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
                      &lt;prop key="hbm2ddl.auto"&gt;validate&lt;/prop&gt;
                  &lt;/props&gt;
              &lt;/property&gt;
          &lt;/bean&gt;
  
          &lt;context:property-placeholder location="classpath:jdbc.properties"/&gt;
          &lt;bean id="dataSource"
                class="org.springframework.jdbc.datasource.DriverManagerDataSource"
                p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}"
                p:username="${jdbc.username}" p:password="${jdbc.password}"/&gt;
      &lt;/beans&gt;
  &lt;/beans&gt;
  </code></pre>