Junit4运行mvn test 测试套件升级方案
·
背景:
项目中包含了junit4(独立引入) 和junit5(spring-boot-starter-parent 2.5.4自带版本)
目前我们使用的springboot 3.5.4自带的maven-surefire-plugin 版本为:2.22.2
其加载逻辑如下: 会按照junit5的规则查找测试,但是我们suit套件使用的是junit4版本,导致出现找不到单元测试异常。
if the JUnit 5 Platform Engine is present in the project
use junit-platform
if the JUnit version in the project >= 4.7 and the <<<parallel>>> configuration parameter has ANY value
use junit47 provider
if JUnit >= 4.0 is present
use junit4 provider
else
use junit3.8.1
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
异常信息:
执行数量为0:[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
或者mvn -Dtest=TestSuiteOne test
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project wdseller-web: No tests were executed! (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
解决方案:
- 第一种方案:全局升级到junit5
- 第二种方案:指定maven-surefire-plugin 到一个老的版本
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<includes>
<include>com.wdtrip.seller.controller.TestSuiteOne</include>
</includes>
</configuration>
</plugin>
其它:suit套件执行方法
- mvn -Dtest=TestSuiteOne test (通过指定测试suit,直接执行)
- 如上pom配置maven-surefire-plugin 的include后,直接执行mvn test ,效果相同
参考文档:
官方文档:Maven Surefire Plugin – Using JUnit
csdn:测试开发基础 mvn test | 利用 Maven Surefire Plugin 做测试用例基础执行管理 - 云+社区 - 腾讯云
Why Your JUnit 5 Tests Are Not Running Under Maven - Spring Framework Guru

更多推荐

所有评论(0)