You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3.0.7集成Spock框架遇测试未找到问题求助

问题根源分析

你的问题核心是Spring Boot的spring-boot-starter-test依赖与Spock测试引擎的桥接配置不匹配,具体原因如下:

  1. 默认测试引擎的识别冲突
    Spring Boot 2.4+版本的spring-boot-starter-test默认引入了JUnit 5(Jupiter)测试引擎依赖。而Spock 2.x及以上版本需要通过spock-junit5作为桥接组件,让JUnit Platform能识别Spock的Specification测试类。如果你的build.gradle只添加了spock-core、未配置spock-junit5,JUnit Jupiter引擎会无法识别Spock测试类,从而出现「找不到测试」的报错。

  2. 测试任务的引擎优先级问题
    保留spring-boot-starter-test时,Gradle测试任务会优先使用JUnit Jupiter引擎,缺少桥接依赖的情况下,Spock测试不会被扫描到。而移除该starter依赖后,测试类路径中只有Spock相关依赖,Gradle会自动切换使用Spock原生测试引擎,此时Specification类能被正常识别运行。

无需移除starter-test的解决方案

如果想保留spring-boot-starter-test同时使用Spock,只需在build.gradle中补充Spock与JUnit 5的桥接依赖,并确保版本兼容:

testImplementation 'org.spockframework:spock-core:2.3-groovy-4.0'
testImplementation 'org.spockframework:spock-junit5:2.3-groovy-4.0'
// 保留Spring Boot测试 starter
testImplementation 'org.springframework.boot:spring-boot-starter-test'

同时确保Gradle测试任务启用JUnit Platform(Spring Boot默认已配置,也可显式声明):

test {
    useJUnitPlatform()
}

注意:Spock版本需要与项目Groovy版本匹配,比如Spock 2.3对应Groovy 4.0,Spock 2.0对应Groovy 3.0,需保持版本一致。

内容的提问来源于stack exchange,提问作者Filip Cacic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 19:22:41