title: How to load Spring Application context in Maven tests
body: <p>I have a <strong>Maven web project</strong> where I want to execute some JUnit integration tests. To achieve that, I want to load the <em>applicationContext.xml</em> file to obtain the Spring injected beans. Also my application context files are located into <code>src/main/resources/app_context</code>, but when executing Maven's lifecycle, it's locating them into <code>WEB-INF/classes/application_context</code>, so I'm able to access them through the classpath.</p>
  
  <pre><code>&lt;resource&gt;
      &lt;directory&gt;src/main/resources/appcontext&lt;/directory&gt;
      &lt;targetPath&gt;${basedir}/src/main/webapp/WEB-INF/classes/application_context&lt;/targetPath&gt;
      &lt;filtering&gt;true&lt;/filtering&gt;
      &lt;includes&gt;
          &lt;include&gt;*/**&lt;/include&gt;
      &lt;/includes&gt;
  &lt;/resource&gt;
  </code></pre>
  
  <p>I have tried to access that context in some ways, but without succeeding. I'm also putting them into Maven's target test directory in that way:</p>
  
  <pre><code>&lt;testResources&gt;
      &lt;testResource&gt;
          &lt;directory&gt;
                  src/main/resources/appcontext
              &lt;/directory&gt;
          &lt;targetPath&gt;${basedir}/src/main/webapp/WEB-INF/classes/application_context&lt;/targetPath&gt;
      &lt;/testResource&gt;
  &lt;/testResources&gt;
  </code></pre>
  
  <p>This is a piece of my Test:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "classpath:**/applicationContext.xml" })
  @Transactional
  public class SystemTest {
  
      @Autowired
      ApplicationContext applicationContext;
  
      @Test
      public void initializeSystemTest() {
              //Test code
  </code></pre>
  
  <p>And that's how I have access to the rest of the application context files from my main one:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-15"?&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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws"
  xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"&gt;
  
      &lt;import resource="classpath:application_context/ApplicationContext*.xml" /&gt;
      &lt;context:annotation-config /&gt;
      &lt;!--========== Spring Data Source ========== --&gt;
      &lt;!--Bean stuff--&gt;
  </code></pre>
  
  <p>Basically I want to have only a copy of each one of the applicationContext files stored in <code>src/main/resources</code> and let Maven copy them for test and execution scopes. However my test classes are unable to find and load them. What I have to do?</p>