You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

Cucumber BOM(7.23+版本)与Spring Boot Starter Test依赖版本对齐方案咨询

Cucumber BOM(7.23+版本)与Spring Boot Starter Test依赖版本对齐方案咨询

兄弟,我刚好踩过这个一模一样的坑!你遇到的Unable to load class 'org.junit.platform.engine.support.discovery.DiscoveryIssueReporter'错误,核心原因就是JUnit Platform Engine的版本冲突
Cucumber 7.23+(你用的7.24.0)明确要求JUnit Platform Engine 1.13.x,但Spring Boot 3.5.4的spring-boot-starter-test默认带的是1.12.2版本。加上你用了Spring的io.spring.dependency-management插件,它会强制优先使用Spring Boot BOM里的版本,直接把Cucumber BOM要求的1.13.3降级成了1.12.2,导致Cucumber需要的新类找不到。

针对你的build.gradle.kts配置,我给你三个可行的解决思路,都是实战验证过的:

方案1:让Spring依赖管理优先认Cucumber BOM的版本

Spring的依赖管理插件默认会优先用Spring Boot BOM的版本,咱们换个顺序,把Cucumber BOM的导入放在前面,让它的版本优先级更高。修改你的配置如下:

plugins {
    id("org.springframework.boot") version "3.5.4"
    id("io.spring.dependency-management") version "1.1.7"
    id("java-library")
}

dependencyManagement {
    imports {
        // 先导入Cucumber BOM,让Spring依赖管理优先用它的版本
        mavenBom(libs.cucumber.bom.get().toString())
        // 再导入Spring Boot BOM
        mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
    }
}

dependencies {
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("io.cucumber:cucumber-java")
    testImplementation("io.cucumber:cucumber-junit-platform-engine")
    testImplementation("org.junit.platform:junit-platform-suite")
    testImplementation("io.cucumber:cucumber-spring")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

这个方案最优雅,因为它是通过调整BOM的导入优先级来解决冲突,不需要硬编码版本号。

方案2:硬指定JUnit Platform Engine的版本(应急用)

如果不想改BOM导入顺序,也可以直接在依赖管理块里强制指定JUnit Platform Engine的版本为Cucumber要求的1.13.3,直接覆盖Spring Boot的版本:

plugins {
    id("org.springframework.boot") version "3.5.4"
    id("io.spring.dependency-management") version "1.1.7"
    id("java-library")
}

dependencyManagement {
    dependencies {
        // 强制指定版本,跳过Spring Boot的版本对齐
        dependency("org.junit.platform:junit-platform-engine:1.13.3")
    }
    imports {
        mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
        mavenBom(libs.cucumber.bom.get().toString())
    }
}

// 下面的dependencies块和你原来的一致即可
dependencies {
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation(platform(libs.cucumber.bom))
    testImplementation("io.cucumber:cucumber-java")
    testImplementation("io.cucumber:cucumber-junit-platform-engine")
    testImplementation("org.junit.platform:junit-platform-suite")
    testImplementation("io.cucumber:cucumber-spring")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

这个方案适合快速应急,但硬编码版本号后期维护起来麻烦,优先选方案1。

方案3:升级Spring Boot到兼容版本

如果你的项目允许升级Spring Boot,那更省心的方式是直接升到和Cucumber 7.23+兼容的版本——比如Spring Boot 3.6.x(如果已经发布)的BOM里会自带JUnit Platform Engine 1.13.x,这样两个BOM的版本就自然对齐了,根本不会有冲突。不过如果暂时没法升级,前面两个方案足够解决问题。

验证是否修复成功

改完配置后,记得跑一下你之前用的依赖分析命令确认版本:

./gradlew -q dependencyInsight --dependency org.junit.platform:junit-platform-engine --configuration testRuntimeClasspath

要是输出里显示org.junit.platform:junit-platform-engine:1.13.3(没有被降级到1.12.2),那就说明配置生效了,再跑测试应该就不会报类找不到的错了。

内容来源于stack exchange

火山引擎 最新活动