求助:解决Spring Boot项目测试运行失败及移除「Automatic loading of test framework implementation dependencies has been deprecated」弃用警告
求助:解决Spring Boot项目测试运行失败及移除「Automatic loading of test framework implementation dependencies has been deprecated」弃用警告
兄弟,我仔细扒了你的项目配置和问题细节,发现几个核心坑导致测试跑不起来还触发了Gradle弃用警告,咱们一步步把这些问题填平:
一、先理清问题根源
- 跨版本依赖冲突:你的项目用了Spring Boot 3.2.0,但测试依赖的
spring-boot-starter-test却是2.7.14,大版本跨着来直接会把测试容器搞崩 - Jakarta EE迁移没做全:Spring Boot 3.x已经彻底从
javax切换到jakarta命名空间了,你还在引用老的javax.persistence-api,这会和Spring Data JPA 3.x的依赖打架 - 测试框架配置冗余:原来的
test任务里同时写了useJUnitPlatform()和useJUnit(),这就是那个弃用警告的直接诱因——Gradle 7+之后只需要指定useJUnitPlatform()就能启用JUnit 5,同时写两个会触发自动加载的弃用逻辑
二、一步步修正配置
1. 重构依赖配置(替换原有的dependencies块)
删掉冲突的javax依赖,全靠Spring Boot的依赖管理自动控版本,别手动硬写版本号给自己挖坑:
dependencies { // 让Spring Boot自动匹配对应版本,不要手动指定3.1.2这类版本号 implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'com.mysql:mysql-connector-j' // 版本交给Spring Boot依赖管理 // 移除javax.persistence-api,Spring Boot 3的starter-data-jpa已经自带Jakarta Persistence compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' annotationProcessor 'org.projectlombok:lombok' // 用和项目同版本的starter-test,只排除老的JUnit 4引擎就行 testImplementation 'org.springframework.boot:spring-boot-starter-test' { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } testImplementation 'org.springframework.security:spring-security-test' // 版本同样交给依赖管理 }
2. 简化测试任务配置(替换原有的test块)
只保留JUnit 5的启用配置,把冗余的useJUnit()删掉:
test { useJUnitPlatform() }
三、修正后的完整build.gradle
plugins { id 'org.springframework.boot' version '3.2.0' id 'io.spring.dependency-management' version '1.1.4' id 'java' } group = 'com.fitnessforge' version = '0.0.1-SNAPSHOT' java { sourceCompatibility = '20' } configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'com.mysql:mysql-connector-j' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } testImplementation 'org.springframework.security:spring-security-test' } tasks.named('bootBuildImage') { builder = 'paketobuildpacks/builder-jammy-base:latest' } test { useJUnitPlatform() }
四、改完之后的效果
- 所有Spring生态依赖版本统一,彻底解决跨版本冲突
- 移除了老的
javax依赖,适配Spring Boot 3的Jakarta EE规范 - 测试框架配置精简,那个烦人的弃用警告会直接消失
- 依赖管理全交给Spring Boot,不用再手动挨个维护版本号
现在你再跑./gradlew test,测试应该能正常启动执行了!
备注:内容来源于stack exchange,提问作者Selias
相关产品推荐
相关产品推荐

