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

升级Spring与Gradle时遭遇CucumberBackendException配置异常

解决Spring Boot 3 + Cucumber 7升级后的io.cucumber.core.backend.CucumberBackendException异常

问题场景

升级到Gradle 7.6、Spring Boot 3.0.2、Java 17后,使用Cucumber 7.11.1运行端到端测试时,抛出异常:

io.cucumber.core.backend.CucumberBackendException: Please annotate a glue class with some context configuration

原配置包含标注@CucumberOptions的测试类EndToEndTestsCoverage,以及标注@ContextConfiguration与@SpringBootTest的配置类,该配置在升级前可正常运行。

核心原因

Spring Boot 3(基于Spring 6)对上下文配置的扫描逻辑更严格,同时Cucumber 7调整了glue类的识别规则——原配置的上下文类未被Cucumber正确纳入扫描范围,导致无法加载Spring上下文。

解决步骤

1. 确保配置类被包含在Cucumber的Glue路径中

检查@CucumberOptions的glue属性,必须明确包含配置类所在的包(或其父包),否则Cucumber无法识别该配置类为上下文提供者。

示例代码:

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/features",
    // 同时包含步骤类和配置类的包
    glue = {"com.example.e2e.steps", "com.example.e2e.config"},
    plugin = {"pretty"}
)
public class EndToEndTestsCoverage {
}

2. 调整配置类的注解组合(适配Spring Boot 3)

Spring Boot 3的@SpringBootTest已内置@ContextConfiguration的功能,无需重复标注。确保配置类仅保留必要注解,并明确指定主应用类(避免自动扫描失效)。

示例配置类:

package com.example.e2e.config;

import org.springframework.boot.test.context.SpringBootTest;
import com.example.YourApplication; // 你的Spring Boot主类

@SpringBootTest(classes = YourApplication.class)
public class CucumberSpringConfig {
    // 空类即可,仅作为Cucumber的glue类提供Spring上下文
}

3. 验证Gradle依赖兼容性

确保Cucumber与Spring Boot的依赖版本无冲突,执行以下Gradle命令检查Spring上下文依赖版本:

./gradlew dependencyInsight --dependency org.springframework:spring-context

确认输出的Spring版本为3.0.2,与Spring Boot 3.0.2一致。

4. 修复IntelliJ IDE运行配置(若IDE执行时出错)

  • 打开IntelliJ的Cucumber运行配置
  • 在Glue字段中添加配置类所在的包路径
  • 确认运行配置的JDK版本为17,与项目配置一致

验证

重新运行Cucumber测试,异常应消失,Spring上下文可正常加载。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 14:55:28