IntelliJ中Cucumber配置报错:需为Glue类添加上下文配置
解决Cucumber无main类时的上下文配置报错
报错原因
Cucumber无法找到带有@CucumberContextConfiguration注解的配置类,因为你的CucumberSpringContextConfiguration不在@CucumberOptions指定的glue扫描路径范围内,导致Spring上下文无法初始化。
解决方法
方法1:扩展glue扫描路径
修改CucumberIntegrationTest的@CucumberOptions,将CucumberSpringContextConfiguration所在的包添加到glue参数中:
@RunWith(Cucumber.class) @CucumberOptions( features = "src/test/resources/feature", plugin = {"pretty", "html:target/cucumber"}, glue = { "com.#####.########.cucumberspringboot.cucumberglue", "com.#####.########.cucumberspringboot" // 替换为配置类实际所在包 } ) public class CucumberIntegrationTest {}
如果配置类和glue代码在同一个父包下,直接写父包路径即可覆盖所有子包,更简洁。
方法2:移动配置类到glue指定包
将CucumberSpringContextConfiguration类直接移动到com.#####.########.cucumberspringboot.cucumberglue包内,这样Cucumber扫描glue路径时会自动识别到该配置类,无需修改glue参数。
方法3:检查依赖版本兼容性
确保cucumber-spring与spring-boot-starter-test的版本匹配:
- Spring Boot 2.x 搭配 Cucumber 6.x 或 7.x
- Spring Boot 3.x 搭配 Cucumber 7.x 及以上版本
版本不兼容可能导致上下文加载逻辑异常,进而触发该报错。
验证
修改完成后,直接运行CucumberIntegrationTest类,Cucumber应该能正常加载Spring上下文并执行测试用例,不再抛出配置类未找到的异常。
内容的提问来源于stack exchange,提问作者JustAnotherNoob
相关产品推荐
相关产品推荐

