40

出于某种原因,我无法让 Maven 2 Surefire 插件执行 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");
  }
}

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

  • 已验证的 Maven 版本:Apache Maven 2.2.1 (r801777; 2009-08-06 20:16:01+0100)
  • 已验证的 Surefire 版本:遵循建议
  • 已验证的 Surefire 版本:检查了我的 Surefire 罐子~/.m2/repository/org/apache/maven/surefire——它们都是 2.4.2 或 2.4.3 版本
  • 做了一个mvn dependency:tree | grep junit以确保我只依赖junit 4.7版

我遇到此问题的模块没有 JUnit 3 测试。

还有什么我想念的吗?

4

13 回答 13

38

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需要 JUnit v3.8.1 的 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是朋友。
  • surefire 不是为开发者天堂而生的(我在查看项目 JIRA 报告时意识到了这一点)。尤其如此,因为如果您使用 Maven,则没有其他选择。

谢谢大家的帮助。不幸的是,没有办法在 Pascal 和 Kaleb 之间分配答案点,但 Kaleb 的使用建议mvn -X帮助我走上了正确的轨道,因此正确的答案点归他所有。

于 2010-01-08T12:33:59.103 回答
28

Surefire 插件根据类路径确定应该使用哪个 JUnit 提供程序。如果类路径上有多个 JUnit 版本,您可以更正类路径以在类路径上只有一个 JUnit 版本(如上所述),或者您可以明确指定要使用的提供程序。例如,在您的(父)POM 中指定以下内容会强制使用最新的提供程序(例如,“surefire-junit47”):

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

但是请注意,Surefire 2.7 改变了它确定运行哪些单元测试类的方式。将 Surefire 2.7(或更高版本)与 JUnit 4 一起使用时的新行为是,任何没有 @Test 注释的测试都将被自动跳过。如果您只有 JUnit 4 单元测试,这可能很棒,但如果您有 JUnit 3 和 4 单元测试的组合,使用“surefire-junit47”提供程序将无法正常工作。在这种情况下,最好明确选择“surefire-junit4”提供程序:

[...]
<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>
[...]
于 2011-04-05T21:24:48.493 回答
13

我不知道您所说的“无法执行”是什么意思,但是明确设置使用的包含是否有帮助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 是否提供任何有用的信息?

于 2010-01-07T17:56:10.623 回答
13

另一个可能的原因可能是这个错误(以“不会修复”关闭):https ://issues.apache.org/jira/browse/SUREFIRE-587

简短摘要:如果名称不以“Test”结尾,则不会选择扩展 TestCase(但不使用注释)的测试。

于 2013-07-04T12:15:25.143 回答
7

对于那些想知道为什么 Maven 不接受 JUnit 测试的可怜人来说。

我将 JUnit 和 TestNG 作为依赖项。但是我希望 failsafe 使用 TestNG 运行我的功能测试,并确保使用 JUnit 运行我的单元测试。

但是,我发现 surefire 正在尝试使用 TestNG 运行我的单元测试,但没有发现任何可运行的东西。我的 JUnit 测试被跳过了。

后来我遇到了这个Maven 问题并将surefire配置为只运行这样的“JUnit”测试:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <properties>
      <property>
        <name>junit</name>
        <value>true</value>
      </property>
    </properties>
  </configuration>
</plugin>

希望这可以帮助某人。

于 2016-09-05T03:04:48.690 回答
5

为了 Google 员工的利益,当我遇到这个问题时,是因为我包含了一个 PowerMock 依赖项,该依赖项引入了 TestNG,导致 SureFire 没有检测到 [TestNG] 测试。

我使用 POM 编辑器的 m2eclipse“依赖层次结构”选项卡来查找依赖关系并右键单击以生成排除项(请参阅下面的 XML)。

为了完整起见(对于那些不使用 m2eclipse 的人),这里是排除依赖项的 XML - 我只是通过看到这些自动生成的标签才发现 Maven 的这个特性:

<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-mockito-release-full</artifactId>
  <version>1.4.9</version>
  <classifier>full</classifier>
  <exclusions>
    <exclusion>
      <artifactId>powermock-module-testng</artifactId>
      <groupId>org.powermock</groupId>
    </exclusion>
  </exclusions>
</dependency>

(在我的情况下,排除“powermock-module-testng”就足够了,但如果它是从其他地方进来的,你可以直接排除它。)

于 2011-08-03T10:32:32.787 回答
4

如果您的依赖项中有 JUnit 5 ( import org.junit.jupiter.api.Test;) 并且您正在尝试运行一些 JUnit4 ( import org.junit.Test;) 测试,请确保包含以下依赖项

<dependency>
  <groupId>org.junit.vintage</groupId>
  <artifactId>junit-vintage-engine</artifactId>
  <scope>test</scope>
</dependency>
于 2021-04-18T11:55:56.050 回答
2

1.) 在 pom.xml 中包含以下肯定插件

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
</configuration>
</plugin>

2.) 默认情况下,surefire 插件从包中选择测试类:- src/test/java/....

所以去构建路径并在类路径中包含测试文件夹,如下所示:

在此处输入图像描述 在此处输入图像描述

3.) 转到--> 运行方式-->Maven 测试

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.081 
s - in com.bnym.dcm.api.controller.AccountControllerTest
[INFO] Running com.bnym.dcm.api.controller.DCMApiControllerTest
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - 
in com.bnym.dcm.api.controller.DCMApiControllerTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 7, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ----------------------------------------------------------------------
--
[INFO] BUILD SUCCESS
于 2017-05-10T10:09:31.653 回答
1

您所做的验证很好,特别是检查您是否使用了 2.3+ 版的 surefire 插件(默认情况下,您将获得带有 maven 2.1超级 POM的 2.4.3 版,所以这应该没问题)并检查您不junit-3.8.1.jar传递依赖关系。

现在,只是为了验证这不是一个“全球性问题”(我不这么认为 TBH),您能否从头开始创建一个项目,例如通过运行:

mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=maven-junit4-testcase

然后更新junit依赖:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.7</version>
  <scope>test</scope>
</dependency>

并将编译器级别配置为1.5+

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.5</source>
    <target>1.5</target>
  </configuration>
</plugin>

最后把你的SimpleTest.javanext toAppTest.java并运行mvn test

如果mvn test该项目的运行正常(并且我希望它可以正常运行),您能否使用您正在使用的 POM 配置更新您的问题(来自有问题的项目)?

于 2010-01-07T17:12:44.527 回答
1

一个小小的改变有趣地帮助了我!

我将类名从 MyClassTest 更改为 TestMyClass,在验证我的父 POM.xml 包含以下行后,我得到了这个想法

<test.include.pattern> **/Test*.java <test.include.pattern/>
于 2014-02-21T11:07:16.620 回答
0

您是否为正确的编译器级别配置了 maven-compile-plugin,例如:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.5</source>
    <target>1.5</target>
  </configuration>
</plugin>

否则 maven 会遇到注释问题

于 2010-01-07T16:39:48.217 回答
0

尝试运行集成测试时遇到了类似的问题。我有一个旧版本的 surefire 插件,它试图运行 TestNG 而不是 jUnit。我将 pom 中的版本号更改为 2.20 并且它有效。

于 2017-05-19T20:01:31.883 回答
0

如果您使用的是 Maven,则文件名很重要

默认情况下,测试文件名需要以“Test”或“Tests”开头或结尾,否则在搜索测试时它们将被忽略。

https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

于 2020-07-28T02:02:54.837 回答