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

IntelliJ运行Spock框架测试出现NPE编译异常问题咨询

IntelliJ环境Spock测试编译异常根因

异常日志

运行测试时Groovyc抛出的编译错误如下:

Groovyc: While compiling [tests of global-tournaments-api-server]:
  Could not instantiate global transform class 
    org.spockframework.compiler.SpockTransform specified at
    jar:file:/C:/Users/user/.m2/repository/org/spockframework/spock-core/2.2-M1-groovy-3.0/spock-core-2.2-M1-groovy-3.0.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation
  because of exception java.lang.NullPointerException:
    Cannot invoke "java.net.URL.toString()"
    because the return value of "java.security.CodeSource.getLocation()" is null

根本原因

  • 版本本身缺陷:引入的spock-core:2.2-M1-groovy-3.0是Spock的里程碑预览版本,并非正式稳定版,其核心AST编译转换类org.spockframework.compiler.SpockTransform的初始化逻辑存在空指针风险:类实例化阶段会主动获取自身jar包所属的CodeSource位置URL,调用toString()方法做路径校验,完全没有做CodeSource.getLocation()返回null的兜底判断。
  • 环境触发条件:IntelliJ内置的Groovy编译链在做增量编译、测试任务类加载时,会使用自定义的类加载器加载AST转换插件,部分场景下不会为加载的spock-core jar包生成带合法位置URL的CodeSource对象,刚好命中上述预览版本的空指针逻辑,导致SpockTransform类无法实例化,编译中断。
  • 该问题和业务代码、测试用例写法无关,属于Spock预览版本和IntelliJ编译环境的兼容性bug,在Spock后续正式稳定版中已经修复了该空指针判断逻辑。

内容的提问来源于stack exchange,提问作者Alessandro Morsiani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.31 13:09:16