Java Selenium Cucumber报错:NoClassDefFoundError(TypeRegistryConfigurerSupplier)
修复Cucumber的
NoClassDefFoundError问题 核心原因
这个错误几乎都是Cucumber相关依赖版本不匹配导致的,TypeRegistryConfigurerSupplier属于Cucumber核心模块,若不同Cucumber组件(如cucumber-java、cucumber-junit、cucumber-core)版本不一致,就会出现类找不到的情况。
解决方案步骤
1. 统一所有Cucumber依赖的版本
确保所有Cucumber相关依赖使用完全相同的版本,避免版本差异引发的兼容问题。示例pom.xml配置(以稳定版7.14.0为例):
<properties> <cucumber.version>7.14.0</cucumber.version> </properties> <dependencies> <!-- Cucumber核心依赖 --> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>${cucumber.version}</version> <scope>test</scope> </dependency> <!-- JUnit测试运行器 --> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-junit</artifactId> <version>${cucumber.version}</version> <scope>test</scope> </dependency> <!-- JUnit基础依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies>
2. 检查并排除冲突依赖
执行Maven命令查看依赖树,找出间接引入的不同版本Cucumber依赖:
mvn dependency:tree | grep cucumber
若发现冲突,在对应依赖中添加排除规则:
<dependency> <!-- 存在冲突的间接依赖 --> <groupId>xxx</groupId> <artifactId>xxx</artifactId> <version>xxx</version> <exclusions> <exclusion> <groupId>io.cucumber</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency>
3. 清理并重新构建项目
删除旧编译文件和依赖缓存,重新构建项目:
mvn clean install -DskipTests
4. 验证TestRunner注解正确性
确保TestRunner类使用对应版本的注解,以JUnit 4为例:
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.steps" ) public class TestRunner { }
若使用JUnit 5,需替换为cucumber-junit-platform-engine依赖,并使用@Cucumber注解。
补充排查点
- 刷新IDE的Maven依赖缓存(如IDEA:右键项目 → Maven → Reload Project)
- 确认
cucumber-core依赖包中确实包含TypeRegistryConfigurerSupplier类
内容的提问来源于stack exchange,提问作者Benjamin josiah
相关产品推荐
相关产品推荐

