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

Karate BDD执行报错java.lang.NoSuchMethodError求排查

问题描述

修改代码后运行测试时抛出java.lang.NoSuchMethodError错误,以下是相关配置、代码及错误堆栈:

依赖配置

<dependency>
    <groupId>com.intuit.karate</groupId>
    <artifactId>karate-core</artifactId>
    <version>${karate.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.intuit.karate</groupId>
    <artifactId>karate-junit5</artifactId>
    <version>${karate.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>net.masterthought</groupId>
    <artifactId>cucumber-reporting</artifactId>
    <version>5.3.1</version>
    <scope>test</scope>
</dependency>
<build>
    <TestSourceDirectory>${project.basedir}/src/test/java</TestSourceDirectory>
    <testSourceDirectory>${TestSourceDirectory}</testSourceDirectory>
    <testResources>
        <testResource>
            <directory>${TestResourceDirectory}</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </testResource>
    </testResources>
</build>

单测运行类代码

package examples.users;

import com.intuit.karate.junit5.Karate;

class UsersRunner {
    
    @Karate.Test
    Karate testUsers() {
        return Karate.run("classpath:/examples/users").relativeTo(getClass());
    }
}

并行测试与报告生成代码

package examples;

import com.intuit.karate.KarateOptions;
import com.intuit.karate.Results;
import com.intuit.karate.Runner;
import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportBuilder;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
class ExamplesTest {

    @Test
    void testParallel() {
        Results results = Runner.path("classpath:examples")
                //.outputCucumberJson(true)
                .parallel(5);
        assertEquals(0, results.getFailCount(), results.getErrorMessages());
    }

    public static void generateReport(String karateOutputPath) {
        Collection<File> jsonFiles = FileUtils.listFiles(new File(karateOutputPath), new String[]{"json"}, true);
        List<String> jsonPaths = new ArrayList<String>(jsonFiles.size());
        jsonFiles.forEach(file -> jsonPaths.add(file.getAbsolutePath()));
        Configuration config = new Configuration(new File("target"), "API Tests");
        ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config);
        reportBuilder.generateReports();
    }
}

错误堆栈信息

java.lang.NoSuchMethodError: 'io.github.classgraph.ClassGraph io.github.classgraph.ClassGraph.acceptPaths(java.lang.String[])'

    at com.intuit.karate.resource.ResourceUtils.<clinit>(ResourceUtils.java:100)
    at com.intuit.karate.Runner$Builder.resolveAll(Runner.java:204)
    at com.intuit.karate.Suite.<init>(Suite.java:177)
    at com.intuit.karate.Runner$Builder.parallel(Runner.java:483)
    at examples.ExamplesTest.testParallel(ExamplesTest.java:23)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)

问题根源与解决方案

根源分析

这个错误是ClassGraph版本冲突导致的:

  • Karate框架依赖的ClassGraph版本包含acceptPaths(String[])方法
  • 项目中引入的cucumber-reporting:5.3.1间接依赖了旧版本ClassGraph,该版本不存在这个方法
  • 构建工具最终引入了旧版本ClassGraph,导致Karate运行时无法找到所需方法

解决方案

方法1:强制指定兼容的ClassGraph版本

在pom.xml中直接引入与你的Karate版本兼容的ClassGraph版本,覆盖间接依赖的旧版本:

<dependency>
    <groupId>io.github.classgraph</groupId>
    <artifactId>classgraph</artifactId>
    <version>4.8.147</version> <!-- 该版本与多数新版Karate兼容,可根据实际Karate版本调整 -->
    <scope>test</scope>
</dependency>

可通过Maven命令查看当前依赖树中的ClassGraph版本:mvn dependency:tree | grep classgraph

方法2:升级cucumber-reporting至兼容版本

将cucumber-reporting升级到6.x及以上版本,这些版本依赖的ClassGraph版本与新版Karate兼容:

<dependency>
    <groupId>net.masterthought</groupId>
    <artifactId>cucumber-reporting</artifactId>
    <version>6.10.0</version> <!-- 选择与你的Karate版本匹配的版本 -->
    <scope>test</scope>
</dependency>

方法3:排除冲突的间接依赖

在cucumber-reporting依赖中排除旧版本ClassGraph,确保项目使用Karate依赖的版本:

<dependency>
    <groupId>net.masterthought</groupId>
    <artifactId>cucumber-reporting</artifactId>
    <version>5.3.1</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>io.github.classgraph</groupId>
            <artifactId>classgraph</artifactId>
        </exclusion>
    </exclusions>
</dependency>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 23:12:46