Karate 1.5.0+Java22配置Surefire/Cucumber报告报错求助
问题解决:Karate 1.5.0 + Java 22 下 SurefireBooterForkException 修复
核心原因
报错'java.util.Set org.junit.platform.engine.TestDescriptor.getAncestors()'源于JUnit Platform 版本与 Maven Surefire 插件版本不兼容:Java 22 对字节码及API做了更新,旧版 Surefire 无法适配 JUnit Platform 1.8+ 中getAncestors()方法返回类型从List改为Set的变更,导致类加载失败。
步骤1:更新 Maven Surefire 插件版本
Karate 1.5.0 依赖较新的 JUnit 5 组件,需搭配支持 Java 22 的 Surefire 版本(3.2.0+)。修改 POM 中的 Surefire 配置:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <!-- 选用兼容Java 22的最新稳定版 --> <version>3.2.5</version> <configuration> <forkCount>1</forkCount> <reuseForks>false</reuseForks> <includes> <include>**/*TestRunner.java</include> </includes> </configuration> <dependencies> <!-- 绑定适配的JUnit Platform提供者 --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>1.10.2</version> </dependency> </dependencies> </plugin> </plugins> </build>
步骤2:对齐 JUnit 5 依赖版本
显式指定与 Surefire 兼容的 JUnit 5 版本,避免依赖冲突:
<dependencies> <!-- Karate 核心测试依赖 --> <dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-junit5</artifactId> <version>1.5.0</version> <scope>test</scope> </dependency> <!-- JUnit 5 引擎与平台依赖 --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.10.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.10.2</version> <scope>test</scope> </dependency> </dependencies>
步骤3:调整测试运行器代码
确保测试运行器使用 Karate 提供的 JUnit 5 注解,摒弃旧版 JUnit 4 风格:
import com.intuit.karate.junit5.Karate; class PortalTestRunner { @Karate.Test Karate runPortalTests() { return Karate.run("portal").relativeTo(getClass()); } }
步骤4:明确 Java 版本配置
在 POM 中指定 Java 22 作为编译和运行环境:
<properties> <maven.compiler.source>22</maven.compiler.source> <maven.compiler.target>22</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
完成上述修改后,重新执行测试即可正常生成 Karate 报告,同时解决 SurefireBooterForkException 问题。
内容的提问来源于stack exchange,提问作者Radim Bukovský
相关产品推荐
相关产品推荐

