JUnit5:Surefire插件两次运行JUnit4测试

时间:2020-10-17 23:50:10

标签: java maven junit5 maven-surefire-plugin

我的项目中同时具有JUnit4和JUnit5测试。问题是,当我运行mvn clean install时,JUnit4测试运行两次(JUnit5测试运行良好且仅运行一次)。

我的父项目pom中具有以下surefire-plugin配置(仅显示相关的依赖项)

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.22.2</version>
  <configuration>
    <threadCount>1</threadCount>
    <properties>
      <property>
        <name>junit</name>
        <value>false</value>
      </property>
    </properties>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-testng</artifactId>
      <version>2.22.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-junit-platform</artifactId>
      <version>2.22.2</version>
    </dependency>
  </dependencies>
</plugin>
...
...
<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-engine</artifactId>
  <version>5.6.2</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.junit.vintage</groupId>
  <artifactId>junit-vintage-engine</artifactId>
  <version>5.6.2</version>
  <scope>test</scope>
</dependency>
<dependency>
  <!-- needed for https://youtrack.jetbrains.com/issue/IDEA-231927?_ga=2.101965186.223349104.1602977709-1646014256.1600106493 -->
  <groupId>org.junit.platform</groupId>
  <artifactId>junit-platform-launcher</artifactId>
  <version>1.6.2</version>
  <scope>test</scope>
</dependency>

我也在子项目中复制了上面的surefire插件,以确保它不会被任何东西覆盖。但是,JUnit4测试仍然运行两次。

以下是有效pom中的surefire-plugin部分-

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.22.2</version>
  <configuration>
    <threadCount>1</threadCount>
    <properties>
      <property>
        <name>junit</name>
        <value>false</value>
      </property>
    </properties>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-testng</artifactId>
      <version>2.22.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-junit-platform</artifactId>
      <version>2.22.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-junit47</artifactId>
      <version>2.19.1</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</plugin

在使用-X选项进行调试时,我认为原因是因为surefire-junit47也被添加到用于surefire插件的提供程序中,surefire-junit-platform运行一次junit4测试,然后运行它们由surefire-junit47提供商再次提供。如果这是可能的原因,那么如何防止将其添加到surefire-plugin依赖项中?我尝试了 <classpathDependencyExcludes>,但这没有帮助,有效的pom仍然包含surefire-junit47。

即使同时具有两个提供者(surefire-junit47surefire-junit-platform),还有什么方法可以避免JUnit4运行两次?

----------更新------------

我还为surefire的配置中将junit属性设置为false,以防止testng提供程序运行junit测试(如建议的here)。但是,我仍然要运行两次JUnit4测试。我的猜测是surefire-junit47(正在神秘地添加)和surefire-junit-platform一起奇怪地动作,导致重复运行。

----------更新------------

实际的问题是我的项目继承了一个父pom,该pom在surefire-plugin中声明了依赖项surefire-junit47。因此,我的项目有效地同时拥有surefire-junit-platform和surefire-junit47,这导致了JUnit4测试的双重运行。

1 个答案:

答案 0 :(得分:1)

我能够重现您的问题。您遇到here描述的情况:

TestNG 6.5.1和更高版本提供了在当前Maven项目中运行TestNG和JUnit 4.x的支持。 (...)

您可能要运行两个提供程序,例如surefire-junit47surefire-testng,并通过设置属性surefire-testng来避免在junit=false提供程序中运行JUnit测试。

因此,请将您的Surefire插件配置更改为:

<configuration>
  <threadCount>1</threadCount>
  <properties>
    <!-- Avoid running JUnit 4 tests in TestNG engine -->
    <property>
      <name>junit</name>
      <value>false</value>
    </property>
  </properties>
</configuration>

更新:如何(伪)排除直接的Maven插件依赖项:

好的,FWIW,我想出了一种巧妙的方法,可以通过用虚拟对象覆盖依赖来“排除”依赖。

首先,将模块添加到项目的根POM,但不将根POM指定为其父级。只要确保该模块内置在同一反应堆中即可。另外,您也可以为其创建一个单独的项目,并确保其结果工件在您公司的存储库中。

模块POM如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.apache.maven.surefire</groupId>
  <artifactId>surefire-junit47</artifactId>
  <version>dummy</version>
</project>

在您希望停用JUnit 4.7引擎以支持在JUnit 5平台上运行JUnit 4测试的子POM中,您可以执行以下操作:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>${maven-surefire.version}</version>
  <configuration>
    <threadCount>1</threadCount>
    <properties>
      <!-- Avoid running JUnit 4 tests in TestNG engine -->
      <property>
        <name>junit</name>
        <value>false</value>
      </property>
    </properties>
  </configuration>
  <dependencies>
    <dependency>
      <!-- Deactivate JUnit 4.7 engine by overriding it with an empty dummy -->
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-junit47</artifactId>
      <version>dummy</version>
    </dependency>
  </dependencies>
</plugin>

<!-- (...) -->

<dependencies>
  <dependency>
    <groupId>org.apache.maven.surefire</groupId>
    <artifactId>surefire-junit47</artifactId>
    <version>dummy</version>
  </dependency>
</dependencies>

这非常丑陋,如果可能的话,我仍然建议重构父POM。无论如何,它是可行的。现在,您的JUnit 4测试仅运行一次,即在JUnit 5平台内与JUnit 5测试,Spock 2.0测试或您拥有的测试一起运行。


更新2::在您提供了MCVE on GitHub之后,我给您发送了一个pull request,它完全实现了我在先前更新中提到的解决方法。重要的提交是this one

相关问题