设置provider解决maven找不到JUnit 5测试样例

问题描述

尝试复现一个用大模型生成测试样例的工作,但使用maven生成的JUnit 5测试样例死活不执行。又不想用命令行运行,因此进行排查

基本知识

<dependencies>

junit-jupiter-api JUnit 5写代码时调用的库
junit-jupyter-engine 运行JUnit 5测试样例的引擎(TestEngine)

<plugins>

maven-surefire-plugin 执行 mvn test时实际执行的插件。它需要一个Provider来提供测试样例,需要一个TestEngine的实现来执行测试样例

问题细节与分析

已经正确设置了<dependencies>,也确认了测试样例的类被正确编译,但就是不执行

分析maven执行轨迹,发现一条:

[INFO] --- surefire:3.3.1:test (default-test) @ tullibee-api ---
[INFO] Using auto detected provider org.apache.maven.surefire.junit4.JUnit4Provider

原来是Provider出错。不知道为什么,maven-surefire-plugin自动选择Provider出错了

解决方法

[INFO] Scanning for projects... [INFO] [INFO] -------------------------< com.li:mybatis-01 >-------------------------- [INFO] Building mybatis-01 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ mybatis-01 --- [INFO] Copying 1 resource from src\main\resources to target\classes [INFO] Copying 1 resource from src\main\java to target\classes [INFO] [INFO] --- compiler:3.13.0:compile (default-compile) @ mybatis-01 --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ mybatis-01 --- [INFO] skip non existing resourceDirectory D:\IDEA\codeData\Mybatis_study\mybatis-01\src\test\resources [INFO] [INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ mybatis-01 --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ mybatis-01 --- [INFO] Using auto detected provider org.apache.maven.surefire.junit4.JUnit4Provider Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-junit4/3.2.5/surefire-junit4-3.2.5.pom Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-junit4/3.2.5/surefire-junit4-3.2.5.pom (3.0 kB at 6.0 kB/s) Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-providers/3.2.5/surefire-providers-3.2.5.pom Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-providers/3.2.5/surefire-providers-3.2.5.pom (2.6 kB at 12 kB/s) Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/common-junit4/3.2.5/common-junit4-3.2.5.pom Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/
最新发布
03-22
### Maven Surefire 插件依赖下载失败解决方案 当遇到 Maven 构建过程中 Surefire 插件依赖无法正常下载的情况时,可以尝试以下方法来解决问题。 #### 1. 验证本地仓库配置 确保本地 `~/.m2/settings.xml` 文件中的镜像设置正确无误。如果使用的是国内镜像源(如阿里云),需确认其地址是否有效并支持所需插件版本。通常情况下,可以通过修改 `<mirrors>` 节点指向可靠的中央仓库替代默认的 Maven Central[^3]。 ```xml <mirrors> <mirror> <id>aliyun</id> <name>Aliyun Mirror</name> <url>https://maven.aliyun.com/repository/public</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> ``` #### 2. 明确指定插件版本号 有时未显式声明插件版本可能导致兼容性问题或者网络请求错误。因此,在项目的根目录下的 `pom.xml` 中加入如下片段以锁定具体使用的 Surefire 版本: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M7</version> <!-- 这里填写稳定版 --> </plugin> </plugins> </build> ``` 此操作有助于避免因自动解析最新可用版本带来的不确定性[^4]。 #### 3. 清理缓存重试 执行命令删除已损坏或不完整的文件后再重新获取资源: ```bash mvn dependency:purge-local-repository clean install ``` 上述指令会先清除所有存储于 `.m2/repository/...` 下的相关条目然后再按需拉取新副本[^5]。 #### 4. 手动安装缺失构件至本地库 假如仍然存在个别难以通过常规手段修复的情形,则可考虑从官方站点或其他可信渠道下载对应 jar 包之后利用 mvn 命令手动引入到环境中去。例如对于 maven-surefire-report 的情况来说就是这样的形式: ```bash mvn install:install-file \ -Dfile=/path/to/maven-surefire-report-x.x.jar \ -DgroupId=org.apache.maven.surefire \ -DartifactId=maven-surefire-report \ -Dversion=x.x \ -Dpackaging=jar ``` 以上措施综合运用基本能够应对大多数关于 surefire plugin download failure 类型事件的发生概率极大降低下来了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值