Junit5执行mvn test失败No tests were executed

在使用Maven 3.6.3和Surefire-plugin 2.12时,执行`mvn -Dtest=TestClass test`遇到'No tests were executed!'错误。尽管测试类能单独运行,但通过Maven运行则不成功。尝试更新Surefire-plugin到2.22.2,添加junit-platform-surefire-provider依赖等方法后,最终通过修改pom.xml使测试用例正常执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

maven版本:3.6.3
surefire-plugin版本:2.12
Junit5

在终端执行mvn -Dtest=TestClass test,显示No tests were executed!,Tests run:0

检查导入的包没有问题,单独运行测试类可正常执行测试用例。

尝试办法1:在-Dtest中加上文件所在位置包名(mvn -Dtest=com.xxx.TestClass test);未解决
尝试办法2:尝试重新加载依赖;未解决
尝试办法3:执行mvn clean,之后再执行test;未解决

搜索之后,发现有文章提到“The Maven Surefire Plugin 2.22.0 (or newer) provides native support for JUnit 5”
尝试办法4:修改surefire-plugin版本为2.22.2;未解决
尝试办法5Why Your JUnit 5 Tests Are Not Running Under Maven; junit-platform-surefire-provider是通过surefire运行junit5很重要的依赖jar;修改pom文件

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<version>2.19.1</version>
	<configuration>
		<skipTests>false</skipTests>
	</configuration>
	<dependencies>
		<dependency>
			<groupId>org.apache.maven.surefire</groupId>
			<artifactId>surefire-junit47</artifactId>
			<version>2.19.1</version>
		</dependency>
		<dependency>
			<groupId>org.junit.platform</groupId>
			<artifactId>junit-platform-surefire-provider</artifactId>
			<version>1.1.0</version>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>5.1.0</version>
		</dependency>
	</dependencies>
</plugin>

再次执行mvn -Dtest=TestClass test,显示Tests run:2,测试用例正常执行。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寅月星回

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值