Surefire 不接受 Junit 4 测试 [英] Surefire is not picking up Junit 4 tests

查看:39
本文介绍了Surefire 不接受 Junit 4 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我无法使用 Maven 2 Surefire 插件来执行 JUnit 4 测试类.

For some reason I cannot get Maven 2 Surefire plugin to execute JUnit 4 test class.

public class SimpleTest {
  @org.junit.Test
  public void simple() {
     System.out.println("foo");
  }
}

但是,如果我将此类更改为 JUnit-3 之类的,例如

However if I change this class to be JUnit-3 like, such as

public class SimpleTest extends junit.framework.TestCase {
  public void testBar() {
     System.out.println("bar");
  }

  @org.junit.Test
  public void simple() {
     System.out.println("foo");
  }
}

然后它被执行.这是我所做的:

then it gets executed. Here's what I've done:

  • 已验证的 Maven 版本:Apache Maven 2.2.1 (r801777; 2009-08-06 20:16:01+0100)
  • 已验证的 Surefire 版本:遵循 这个建议
  • 已验证的 Surefire 版本:在我的 ~/.m2/repository/org/apache/maven/surefire 中检查了 Surefire jars -- 它们都是 2.4.2 或 2.4.3 版
  • 做了一个mvn dependency:tree |grep junit 确保我只依赖于 junit 4.7 版
  • verified Maven version: Apache Maven 2.2.1 (r801777; 2009-08-06 20:16:01+0100)
  • verified Surefire version: followed this advice
  • verified Surefire version: checked Surefire jars in my ~/.m2/repository/org/apache/maven/surefire -- all of them are either version 2.4.2 or 2.4.3
  • done a mvn dependency:tree | grep junit to ensure I only depend on junit version 4.7

我遇到这个问题的模块没有 JUnit 3 测试.

The module I am having this problem at doesn't have JUnit 3 tests.

还有什么我遗漏的吗?

推荐答案

mvn -X 帮助我揭示了以下内容:

mvn -X helped me to reveal the following:

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

所以问题似乎来自需要 JUnit v3.8.1 的 testng jar.尽管 Test Classpath 被设置为依赖于 JUnit 4,但为时已晚.

So it seems that the problem was coming from testng jar requiring JUnit v3.8.1. Even though Test Classpath was set to depend on JUnit 4, it was too late.

testng 依赖项位于我的 POM 中:

testng dependency was located in my POM:

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>5.8</version>
  <scope>test</scope>
  <classifier>jdk15</classifier>
</dependency>

在我将其注释掉后,测试立即开始执行.

Immediately after I have commented it out, tests started to execute.

经验教训:

  • mvn dependency:tree 并不总是足够的,mvn -X 是一个朋友.
  • surefire 不是为开发者天堂而生的(我在查看 JIRA 项目报告时意识到了这一点).尤其如此,因为如果您使用 Maven,则没有其他选择.
  • mvn dependency:tree is not always enough, mvn -X is a friend.
  • surefire is not made for developer heaven (I have realized this while looking at project JIRA reports). This is especially true as there are no other alternatives if you use Maven.

感谢大家的帮助.不幸的是,无法在 Pascal 和 Kaleb 之间拆分答案点,但 Kaleb 建议使用 mvn -X 帮助我走上正确的轨道,因此正确的答案点交给了他.

Thanks everybody for your help. Unfortunately there is no way to split answer points between Pascal and Kaleb, but Kaleb's advice to use mvn -X helped me to get on the right track so correct answer points go to him.

这篇关于Surefire 不接受 Junit 4 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆