由于某些原因,我无法让Maven 2尽力而为的插件来执行JUnit 4测试类。
public class SimpleTest {
@org.junit.Test
public void simple() {
System.out.println("foo");
}
}但是,如果我将这个类更改为JUnit-3,如
public class SimpleTest extends junit.framework.TestCase {
public void testBar() {
System.out.println("bar");
}
@org.junit.Test
public void simple() {
System.out.println("foo");
}
}然后执行。以下是我所做的:
~/.m2/repository/org/apache/maven/surefire中检查过的安全容器--所有这些都是版本2.4.2或2.4.3mvn dependency:tree | grep junit,以确保我只依赖junVersion4.7我遇到这个问题的模块没有JUnit 3测试。
我还遗漏了什么吗?
发布于 2010-01-08 12:33:59
mvn -X帮助我展示了以下内容:
...
[INFO] [surefire:test {execution: default-test}]
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] org.apache.maven.surefire:surefire-booter:jar:2.4.3:runtime (selected for runtime)
[DEBUG] org.apache.maven.surefire:surefire-api:jar:2.4.3:runtime (selected for runtime)
[DEBUG] Adding to surefire booter test classpath: /home/mindas/.m2/repository/org/apache/maven/surefire/surefire-booter/2.4.3/surefire-booter-2.4.3.jar
[DEBUG] Adding to surefire booter test classpath: /home/mindas/.m2/repository/org/apache/maven/surefire/surefire-api/2.4.3/surefire-api-2.4.3.jar
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] org.testng:testng:jar:jdk15:5.8:test (selected for test)
[DEBUG] junit:junit:jar:3.8.1:test (selected for test)
[DEBUG] Adding to surefire booter test classpath: /home/mindas/.m2/repository/org/testng/testng/5.8/testng-5.8-jdk15.jar
[DEBUG] Adding to surefire booter test classpath: /home/mindas/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] Retrieving parent-POM: org.apache.maven.surefire:surefire-providers:pom:2.4.3 for project: null:surefire-testng:jar:null from the repository.
[DEBUG] Adding managed dependencies for unknown:surefire-testng
[DEBUG] org.apache.maven.surefire:surefire-api:jar:2.4.3
[DEBUG] org.apache.maven.surefire:surefire-booter:jar:2.4.3
[DEBUG] org.codehaus.plexus:plexus-utils:jar:1.5.1
[DEBUG] org.apache.maven.surefire:surefire-testng:jar:2.4.3:test (selected for test)
[DEBUG] org.apache.maven:maven-artifact:jar:2.0:test (selected for test)
[DEBUG] org.codehaus.plexus:plexus-utils:jar:1.0.4:test (selected for test)
[DEBUG] junit:junit:jar:3.8.1:test (selected for test)
[DEBUG] org.testng:testng:jar:jdk15:5.7:test (selected for test)
[DEBUG] org.apache.maven.surefire:surefire-api:jar:2.4.3:test (selected for test)
...
[DEBUG] Test Classpath :
...
[DEBUG] /home/mindas/.m2/repository/junit/junit/4.7/junit-4.7.jar因此,问题似乎来自于需要testng v3.8.1的JUnit jar。尽管Test Classpath被设置为依赖于JUnit 4,但为时已晚。
testng依赖项位于POM中:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.8</version>
<scope>test</scope>
<classifier>jdk15</classifier>
</dependency>在我将其注释掉之后,立即开始执行测试。
汲取的经验教训:
mvn dependency:tree并不总是足够的,mvn -X是一个朋友。谢谢大家的帮助。不幸的是,帕斯卡和卡莱布之间没有办法把答案分分开,但是卡莱布关于使用mvn -X的建议帮助我走上了正确的轨道,所以正确的回答点给了他。
发布于 2011-04-05 21:24:48
该插件确定了应该根据类路径使用哪个JUnit提供者。如果类路径上有多个JUnit版本,则可以将类路径更正为在类路径上只有一个JUnit版本(如上所述),也可以显式指定要使用哪个提供程序。例如,在您的(父级) POM中使用最新的提供者(例如,“尽力而为-军47”)指定以下内容:
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8</version>
<dependencies>
<!-- Force using the latest JUnit 47 provider -->
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.8</version>
</dependency>
</dependencies>
[...]但是,请注意,照射器2.7改变了它决定运行哪个单元测试类的方式。在JUnit 4中使用JUnit 2.7 (或更高版本)时的新行为是,任何没有@Test注释的测试都将自动跳过。如果您只进行JUnit 4单元测试,那么这可能是很棒的,但是如果您将JUnit 3和4单元测试结合在一起,那么使用“照管47”提供程序将无法正常工作。在这种情况下,最好显式地选择“尽职尽责-4”提供者:
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<!-- Use the older JUnit 4 provider -->
<artifactId>surefire-junit4</artifactId>
<version>2.8</version>
</dependency>
</dependencies>
[...]发布于 2010-01-07 17:56:10
我不知道您所说的“不能执行”是什么意思,但是显式设置maven-surefire-plugin使用的包含是否有帮助?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>此外,使用-X标志运行maven是否提供了任何有用的信息?
https://stackoverflow.com/questions/2021771
复制相似问题