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

如何解决JUnit5:Surefire插件两次运行JUnit4测试

我的项目中同时具有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测试的双重运行。

解决方法

我能够重现您的问题。您遇到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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-