【问题标题】:springTestContextBeforeTestMethod failed in Maven spring-testspringTestContextBeforeTestMethod在Maven spring-test中失败
【发布时间】:2010-05-25 07:34:16
【问题描述】:

我尝试在 Maven 中使用 TestNg 设置一个带有 spring-test 的项目。代码如下:

@ContextConfiguration(locations={"test-context.xml"})
public class AppTest extends AbstractTestNGSpringContextTests {

    @Test
    public void testApp() {
        assert true;
    }
}

一个 test-context.xml 只定义了一个 bean:

<bean id="app" class="org.sonatype.mavenbook.simple.App"/>

从命令行运行 mvn test 时出现 Failed to load ApplicationContext 错误,似乎找不到 test-context.xml 文件;但是,我可以让它在 Eclipse 中正确运行(使用 TestNg 插件)。

那么,test-context.xml 位于 src/test/resources/ 下,我如何在 pom.xml 中指出这一点,以便“mvn test”命令起作用? 谢谢,

更新:

感谢您的回复。无法加载上下文文件错误是由于我将文件移动到不同的位置引起的,因为我认为类路径是问题所在。现在我发现上下文文件似乎是从 Maven 输出中加载的,但是测试失败了:

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 <<< 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

如果我使用spring-test 3.0.2.RELEASE版本,错误变成:

org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance() is depending on nonexistent method null

这是项目的结构:

simple
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    `-- test
        |-- java
        `-- resources
            |-- test-context.xml
            `-- testng.xml

testng.xml:

<suite name="Suite" parallel="false">
  <test name="Test">
    <classes>
      <class name="org.sonatype.mavenbook.simple.AppTest"/>
    </classes>
  </test>
</suite>

测试上下文.xml:

 <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">
    <bean id="app" class="org.sonatype.mavenbook.simple.App"/>
 </beans>

在 pom.xml 中,我添加了 testng、spring 和 spring-test 工件以及插件:

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>5.1</version>
  <classifier>jdk15</classifier>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring</artifactId>
  <version>2.5.6</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId> 
    <version>2.5.6</version> 
    <scope>test</scope> 
</dependency>

<build>
<finalName>simple</finalName>
<plugins>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.6</source>
      <target>1.6</target>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>   
      <suiteXmlFiles>
        <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
      </suiteXmlFiles>
    </configuration>
  </plugin>
</plugins>

基本上,我用 TestNg 替换了 'A Simple Maven Project' Junit,希望它有效。

更新:

我想我遇到了问题(仍然不知道为什么)- 每当我扩展 AbstractTestNGSpringContextTests 或 AbstractTransactionalTestNGSpringContextTests 时,测试都会失败并出现以下错误:

Failed tests:
  springTestContextBeforeTestMethod(org.sonatype.mavenbook.simple.AppTest)
  springTestContextAfterTestMethod(org.sonatype.mavenbook.simple.AppTest)

所以,当我覆盖这两种方法时,错误最终消失了。 我认为这不是正确的方法,没有从 spring-test 文档中找到太多信息。 如果您知道 Spring 测试框架,请对此有所了解。

【问题讨论】:

    标签: spring maven-2 testing testng


    【解决方案1】:

    在文件名前添加类路径。然后它将在所有源文件夹的根文件夹中搜索。

    @ContextConfiguration(locations={"classpath:test-context.xml"})
    

    【讨论】:

    • 我不明白为什么你必须添加classpath:,测试和test-context.xml 都在同一个位置(target/test-classes),应该是什么其他源文件夹搜索了吗?
    • 我也明白类和资源复制到目标文件夹。我只是试图回答您如何确保 Spring 容器将在根文件夹中的上下文之后进行扫描。如果没有classpath:,Spring 将开始扫描带有测试类的包。我希望这能澄清我想说的话。
    • 好的,现在知道了,感谢您的澄清。但我想知道为什么它在没有classspath: 的情况下在 Eclipse 下工作
    • 我不确定 Maven 和 TestNg 是如何完成这项工作的。我正在使用 Ant 和 JUnit。但我的建议至少会消除关于 Spring 将在何处寻找上下文的不确定性。
    • 实际上,我认为这并不能回答问题。
    【解决方案2】:

    我在 Spring 2.5.6 中遇到了同样的问题。我使用 testng 5.5 解决了。

    我使用的版本(testng 5.1)具有相同的行为:测试在 Eclipse 内运行良好,但无法从命令行找到上下文。

    用 testng 2.5.6 替换 testng 问题就解决了。仅适用于 Spring 2.5.6

    【讨论】:

      【解决方案3】:

      因为我还没有看到你的 Java 代码,所以在黑暗中开枪:你确定你使用的是 @BeforeMethod 而不是 @BeforeTest 吗?

      【讨论】:

      • Class App 只是一个虚拟类。测试java代码显示在本文开头:@ContextConfiguration(locations={"/test-context.xml"}) public class AppTest extends AbstractTestNGSpringContextTests { @Test public void testApp() { assert true; } } 只有一个 @Test 方法,没有别的。
      猜你喜欢
      • 2014-10-29
      • 2016-09-30
      • 2019-07-22
      • 2018-05-06
      • 2018-07-18
      • 2022-11-25
      • 2013-10-29
      • 1970-01-01
      • 2021-07-03
      相关资源
      最近更新 更多