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

使用@RunWith(CucumberWithSerenity.class)运行Maven测试抛NoClassDefFoundError如何解决

问题根因及解决方案

根因分析

核心是依赖版本不兼容+重复依赖冲突,具体原因如下:

  • 新旧Cucumber坐标混用:同时引入了新坐标io.cucumber系列的7.0.0版本,和早已停止维护的旧坐标info.cukes:cucumber-core:1.2.6,二者包结构、类定义完全不兼容,直接引发类加载冲突。
  • Cucumber和Serenity版本不匹配:使用的Serenity 3.1.10版本仅适配Cucumber 6.x系列,自行引入的Cucumber 7.0.0对核心类做了重构,io.cucumber.core.runtime.TypeRegistryConfigurerSupplier这个类在Cucumber 7中已经被调整/移除,Serenity 3.1.10找不到对应类才抛出NoClassDefFoundError。
  • 冗余依赖重复引入:serenity-cucumber依赖本身已经传递引入了适配版本的cucumber-java、cucumber-junit等核心包,额外手动引入高版本的Cucumber包,直接覆盖了Serenity适配的正确版本,加剧了冲突。
  • 用原生@RunWith(Cucumber.class)时运行正常,是因为此时直接调用的是手动引入的Cucumber 7的类,不需要Serenity依赖的旧版本Cucumber类,切换到CucumberWithSerenity后需要调用Serenity适配版本的Cucumber核心类,就触发了类找不到的错误。

修复方案

修改pom.xml的依赖配置即可:

  1. 删除以下三个冗余的Cucumber相关依赖:
<!-- 移除以下三个依赖配置 -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>7.0.0</version>
</dependency>
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>7.0.0</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-core</artifactId>
    <version>1.2.6</version>
    <type>pom</type>
</dependency>
  1. 执行mvn clean install -U清理缓存并强制更新依赖,serenity-cucumber会自动引入适配好的对应版本Cucumber包,不需要手动额外添加。
  2. (可选优化)引入Serenity BOM统一管理所有Serenity相关依赖版本,避免后续出现版本不匹配问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 22:06:00