title: springTestContextBeforeTestMethod failed in Maven spring-test
body: <p>I try to setup a project with spring-test using TestNg in Maven. The code is like:</p>
  
  <pre><code>@ContextConfiguration(locations={"test-context.xml"})
  public class AppTest extends AbstractTestNGSpringContextTests {
  
      @Test
      public void testApp() {
          assert true;
      }
  }
  </code></pre>
  
  <p>A test-context.xml simply defined a bean:</p>
  
  <pre><code>&lt;bean id="app" class="org.sonatype.mavenbook.simple.App"/&gt;
  </code></pre>
  
  <p>I got error for Failed to load ApplicationContext when running mvn test from command line, seems it cannot find the test-context.xml file; however, I can get it run correctly inside Eclipse (with TestNg plugin).</p>
  
  <p>So, test-context.xml is under src/test/resources/, how do I indicate this in the pom.xml so that 'mvn test' command will work?
  Thanks,</p>
  
  <h2>UPDATE:</h2>
  
  <p>Thanks for the reply. Cannot load context file error was caused by I moved the file arround in different location since I though the classpath was the problem. Now I found the context file seems loaded from the Maven output, but the test is failed:</p>
  
  <pre><code>Running TestSuite
  May 25, 2010 9:55:13 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  INFO: Loading XML bean definitions from class path resource [test-context.xml]
  May 25, 2010 9:55:13 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
  INFO: Refreshing org.springframework.context.support.GenericApplicationContext@171bbc9: display name [org.springframework.context.support.GenericApplicationContext@171bbc9]; startup date [Tue May 25 09:55:13 PDT 2010]; root of context hierarchy
  May 25, 2010 9:55:13 AM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
  INFO: Bean factory for application context [org.springframework.context.support.GenericApplicationContext@171bbc9]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1df8b99
  May 25, 2010 9:55:13 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
  INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1df8b99: defining beans [app,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor]; root of factory hierarchy
  Tests run: 3, Failures: 2, Errors: 0, Skipped: 1, Time elapsed: 0.63 sec &lt;&lt;&lt; FAILURE!
  
  Results :
  
  Failed tests: 
    springTestContextBeforeTestMethod(org.sonatype.mavenbook.simple.AppTest)
    springTestContextAfterTestMethod(org.sonatype.mavenbook.simple.AppTest)
  
  Tests run: 3, Failures: 2, Errors: 0, Skipped: 1
  </code></pre>
  
  <p>If I use spring-test version 3.0.2.RELEASE, the error becomes:</p>
  
  <pre><code>org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance() is depending on nonexistent method null
  </code></pre>
  
  <p>Here is the structure of the project:</p>
  
  <pre><code>simple
  |-- pom.xml
  `-- src
      |-- main
      |   `-- java
      `-- test
          |-- java
          `-- resources
              |-- test-context.xml
              `-- testng.xml
  </code></pre>
  
  <p>testng.xml:</p>
  
  <pre><code>&lt;suite name="Suite" parallel="false"&gt;
    &lt;test name="Test"&gt;
      &lt;classes&gt;
        &lt;class name="org.sonatype.mavenbook.simple.AppTest"/&gt;
      &lt;/classes&gt;
    &lt;/test&gt;
  &lt;/suite&gt;
  </code></pre>
  
  <p>test-context.xml:</p>
  
  <pre><code> &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
      default-lazy-init="true"&gt;
      &lt;bean id="app" class="org.sonatype.mavenbook.simple.App"/&gt;
   &lt;/beans&gt;
  </code></pre>
  
  <p>In the pom.xml, I add testng, spring, and spring-test artifacts, and plugin:</p>
  
  <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;org.testng&lt;/groupId&gt;
    &lt;artifactId&gt;testng&lt;/artifactId&gt;
    &lt;version&gt;5.1&lt;/version&gt;
    &lt;classifier&gt;jdk15&lt;/classifier&gt;
    &lt;scope&gt;test&lt;/scope&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring&lt;/artifactId&gt;
    &lt;version&gt;2.5.6&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-test&lt;/artifactId&gt; 
      &lt;version&gt;2.5.6&lt;/version&gt; 
      &lt;scope&gt;test&lt;/scope&gt; 
  &lt;/dependency&gt;
  
  &lt;build&gt;
  &lt;finalName&gt;simple&lt;/finalName&gt;
  &lt;plugins&gt;
    &lt;plugin&gt;
      &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
      &lt;configuration&gt;
        &lt;source&gt;1.6&lt;/source&gt;
        &lt;target&gt;1.6&lt;/target&gt;
      &lt;/configuration&gt;
    &lt;/plugin&gt;
    &lt;plugin&gt;
      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
      &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
      &lt;configuration&gt;   
        &lt;suiteXmlFiles&gt;
          &lt;suiteXmlFile&gt;src/test/resources/testng.xml&lt;/suiteXmlFile&gt;
        &lt;/suiteXmlFiles&gt;
      &lt;/configuration&gt;
    &lt;/plugin&gt;
  &lt;/plugins&gt;
  </code></pre>
  
  <p></p>
  
  <p>Basically, I replaced 'A Simple Maven Project' Junit with TestNg, hope it works.</p>
  
  <h2>UPDATE:</h2>
  
  <p>I think I got the problem (still don't know why) -
  Whenever I extends AbstractTestNGSpringContextTests or AbstractTransactionalTestNGSpringContextTests, the test will failed with this error:</p>
  
  <pre><code>Failed tests:
    springTestContextBeforeTestMethod(org.sonatype.mavenbook.simple.AppTest)
    springTestContextAfterTestMethod(org.sonatype.mavenbook.simple.AppTest)
  </code></pre>
  
  <p>So, eventually the error went away when I override the two methods.
  I don't think this is the right way, didn't find much info from spring-test doc. 
  If you know spring test framework, please shred some light on this.</p>