这是一个同时使用 TestNG 和 JUnit4 的项目。出于任何原因,我无法运行 TestNG 测试套件。错误堆栈跟踪如下所示。
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Exception in thread "ThreadedStreamConsumer" java.lang.NoSuchMethodError: org.ap
ache.maven.surefire.report.StackTraceWriter.getThrowable()Ljava/lang/Throwable;
at org.apache.maven.surefire.report.XMLReporter.writeTestProblems(XMLRep
orter.java:272)
at org.apache.maven.surefire.report.XMLReporter.testFailed(XMLReporter.j
ava:249)
at org.apache.maven.surefire.report.MulticastingReporter.testFailed(Mult
icastingReporter.java:80)
at org.apache.maven.surefire.report.TestSetRunListener.testFailed(TestSe
tRunListener.java:157)
at org.apache.maven.plugin.surefire.booterclient.output.ForkClient.consu
meLine(ForkClient.java:106)
at org.apache.maven.plugin.surefire.booterclient.output.ThreadedStreamCo
nsumer$Pumper.run(ThreadedStreamConsumer.java:67)
at java.lang.Thread.run(Thread.java:722)
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
我的pom设置如下
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org /2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<prerequisites>
<maven>3.0.4</maven>
</prerequisites>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<surefire.version>2.13</surefire.version>
</properties>
<parent>
<groupId>com.google</groupId>
<artifactId>google</artifactId>
<version>1</version>
</parent>
<groupId>com.google.testing</groupId>
<artifactId>test-libraries-for-java</artifactId>
<version>1.2</version>
<profiles>
<profile>
<id>junit4-only</id>
<build>
<finalName>tl4j</finalName>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>junit4-only-${project.version}</classifier>
<includes>
<include>**/junit4/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>agnostic</id>
<build>
<finalName>tl4j</finalName>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>agnostic-${project.version}</classifier>
<includes>
<include>**/common/testing/*.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>junit4</id>
<build>
<finalName>tl4j</finalName>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>junit4-${project.version}</classifier>
<includes>
<include>**/common/testing/*.class</include>
<include>**/junit4/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>testng</id>
<build>
<finalName>tl4j</finalName>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>testng-${project.version}</classifier>
<includes>
<include>**/common/testing/*.class</include>
<include>**/testng/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>testng-only</id>
<build>
<finalName>tl4j</finalName>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>testng-${project.version}</classifier>
<includes>
<include>**/testng/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<type>maven-plugin</type>
</dependency>
</dependencies>
<build>
<!-- <finalName>tl4j</finalName> -->
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<outputDirectory>${basedir}/target-eclipse/classes</outputDirectory>
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>deploy</id>
<phase>deploy</phase>
<configuration>
<tasks>
<echo
message="Please follow directions at doc/HOW_TO_RELEASE.txt to make the release" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>${surefire.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<configuration>
<show>private</show>
<nohelp>true</nohelp>
</configuration>
</plugin>
</plugins>
</reporting>
我的单元测试
import org.testng.Assert;
import org.testng.annotations.Test;
public class TearDownTestCaseTest {
@Test
public void assertTrue() {
Assert.assertTrue(true);
}
}
有趣的是,如果我将此测试配置为作为 JUnit 3 测试用例运行,它可以工作,但 JUnit 4.x 或 Testng 不行。