title: Failed to load Spring ApplicationContext
body: <p>I'm writing unit tests for a spring application which is sort of complex. I want to load spring context in order to use defined beans. My context.xml is located at:</p>
  
  <pre><code>src/main/resources/context.xml
  </code></pre>
  
  <p>After maven build, the context.xml appears at:</p>
  
  <pre><code>target/classes/context.xml
  </code></pre>
  
  <p>In the pom.xml, I have: (As suggested by <a href="https://stackoverflow.com/questions/10104372/testing-with-spring-and-maven-applicationcontext">this post</a>)</p>
  
  <pre><code>&lt;resources&gt;
      &lt;resource&gt;
          &lt;filtering&gt;true&lt;/filtering&gt;
          &lt;directory&gt;src/test/resources&lt;/directory&gt;
          &lt;includes&gt;
              &lt;include&gt;**/*.properties&lt;/include&gt;
          &lt;/includes&gt;
          &lt;excludes&gt;
              &lt;exclude&gt;**/*local.properties&lt;/exclude&gt;
          &lt;/excludes&gt;
      &lt;/resource&gt;
      &lt;resource&gt;
          &lt;directory&gt;src/main/resources&lt;/directory&gt;
          &lt;includes&gt;
              &lt;include&gt;**/*.properties&lt;/include&gt;
              &lt;include&gt;**/*.xml&lt;/include&gt;
          &lt;/includes&gt;
      &lt;/resource&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>I have tried to do this job in two ways:</p>
  
  <pre><code>1, Use ApplicationContext
  AppliactionContext springContext = new ClassPathXmlApplicationContext("/context.xml");
  MyObject myObject = springContext.getBean(myObject.class);
  
  2, Use Annotations
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations={"/context.xml"})
  public class myTest{
      @Autowired
      MyObject myObject;
      ...
  }
  </code></pre>
  
  <p>But neither way works for me. Error message:</p>
  
  <pre><code>java.lang.IllegalStateException: Failed to load ApplicationContext
  at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
  at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
  at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
  at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
  at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
  ...  more
  </code></pre>
  
  <p><strong>Important point:</strong> The context.xml was actually copied from another project. These projects run together as an application but when function as Junit, I don't know how to load the context from a different project so I simply copied and pasted the file. This might be a problem</p>
  
  <p><strong>More information</strong>
  <strong>Failure Trace:</strong></p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean
  with name 'validator' defined in class path resource [context.xml]: BeanPostProcessor
  before instantiation of bean failed; nested exception is    
  java.lang.NoClassDefFoundError: org.aspectj.lang.annotation.Aspect
  
  Caused by: java.lang.NoClassDefFoundError: org.aspectj.lang.annotation.Aspect
  
  Caused by: java.lang.ClassNotFoundException: org.aspectj.lang.annotation.Aspect
  </code></pre>
  
  <p>So looks like I have to import some spring aop package first?</p>
  
  <p>Please give suggestions. Much appreciation.</p>