【发布时间】:2020-10-17 23:50:10
【问题描述】:
我的项目中有 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-plugin,以确保它不会被任何东西覆盖。但是,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-plugin 的提供程序中,surefire-junit-platform 运行一次 junit4 测试,它们由surefire-junit47 提供程序再次运行.如果这是一个可能的原因,那么我该如何防止它被添加到 surefire-plugin 依赖项中?我尝试了 <classpathDependencyExcludes>,但这并没有帮助,有效的 pom 仍然包含surefire-junit47。
即使有两个提供程序(surefire-junit47 和 surefire-junit-platform),还有什么方法可以避免 JUnit4 运行两次?
---------更新------------
我还在配置中将junit 属性设置为false,以防止testng 提供程序运行junit 测试(建议here)。但是,我仍然进行了两次 JUnit4 测试。我的猜测是,不知何故 surefire-junit47(它被神秘地添加了)和 surefire-junit-platform 一起表现得很奇怪,导致重复运行。
---------更新------------
实际的问题是我的项目继承了一个父 pom,它在surefire-plugin 中声明了一个依赖surefire-junit47。如此有效地,我的项目同时具有surefire-junit-platform 和surefire-junit47,这导致了JUnit4 测试的双重运行。
【问题讨论】:
标签: java maven junit5 maven-surefire-plugin