我尝试在 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 替换了“一个简单的 Maven 项目”Junit,希望它有效。
更新:
我想我遇到了问题(仍然不知道为什么) - 每当我扩展 AbstractTestNGSpringContextTests 或 AbstractTransactionalTestNGSpringContextTests 时,测试都会失败并出现以下错误:
Failed tests:
springTestContextBeforeTestMethod(org.sonatype.mavenbook.simple.AppTest)
springTestContextAfterTestMethod(org.sonatype.mavenbook.simple.AppTest)
因此,当我覆盖这两种方法时,错误最终消失了。我认为这不是正确的方法,没有从 spring-test 文档中找到太多信息。如果您知道弹簧测试框架,请对此有所了解。