Spring Boot 3下Cucumber上下文配置未被识别的问题求助
问题分析
错误提示明确指出Cucumber无法找到Spring上下文配置类,核心原因是带有@CucumberContextConfiguration的类不在Cucumber的glue扫描范围内:你的测试入口类ShopApplicationTests位于com.alten.shop包,但@CucumberOptions的glue仅指定了com.alten.shop.steps,导致Cucumber扫描不到Spring配置。此外,你同时混用了JUnit 4(@RunWith)和JUnit 5(cucumber-junit-platform-engine)的依赖,可能引发版本冲突。
解决方案
方案一:扩展Glue扫描范围,包含配置类所在包
修改ShopApplicationTests的@CucumberOptions,将配置类所在的com.alten.shop包加入glue列表:
package com.alten.shop; import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions; import io.cucumber.spring.CucumberContextConfiguration; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; @RunWith(Cucumber.class) @CucumberOptions( monochrome = true, features = { "classpath:features/products.feature" }, glue = { "com.alten.shop", "com.alten.shop.steps" }, // 新增配置类所在包 plugin = {"pretty", "json:target/cucumber-report.json"} ) @CucumberContextConfiguration @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class ShopApplicationTests { }
方案二:创建独立的Cucumber配置类(更规范)
在com.alten.shop.steps包下新建Spring配置类,让Cucumber自动扫描到:
package com.alten.shop.steps; import io.cucumber.spring.CucumberContextConfiguration; import org.springframework.boot.test.context.SpringBootTest; @CucumberContextConfiguration @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class CucumberSpringConfig { }
随后修改原测试入口类,移除Spring相关注解:
package com.alten.shop; import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions( monochrome = true, features = { "classpath:features/products.feature" }, glue = { "com.alten.shop.steps" }, plugin = {"pretty", "json:target/cucumber-report.json"} ) public class ShopApplicationTests { }
优化JUnit依赖(适配Spring Boot 3)
Spring Boot 3默认使用JUnit 5,建议统一依赖版本:
- 移除JUnit 4的
cucumber-junit依赖,保留JUnit 5的cucumber-junit-platform-engine - 恢复
spring-boot-starter-test的默认依赖,去掉对junit-jupiter-engine的排除
修改后的pom.xml依赖片段:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>${cucumber.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-spring</artifactId> <version>${cucumber.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-junit-platform-engine</artifactId> <version>${cucumber.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.14</version> </dependency> </dependencies>
- 改用JUnit 5的Suite注解替代
@RunWith(可选,更贴合Spring Boot 3生态):
package com.alten.shop; import org.junit.platform.suite.api.ConfigurationParameter; import org.junit.platform.suite.api.IncludeEngines; import org.junit.platform.suite.api.Suite; import static io.cucumber.junit.platform.engine.Constants.*; @Suite @IncludeEngines("cucumber") @ConfigurationParameter(key = FEATURES_PROPERTY_NAME, value = "classpath:features/products.feature") @ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.alten.shop.steps") @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty,json:target/cucumber-report.json") @ConfigurationParameter(key = MONOCHROME_PROPERTY_NAME, value = "true") public class ShopApplicationTests { }
验证步骤
- 清理项目编译目录(如
target) - 重新运行Cucumber测试
- 检查是否成功启动Spring上下文并执行测试步骤
内容的提问来源于stack exchange,提问作者jozinho22
相关产品推荐
相关产品推荐

