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

Eclipse运行Karate Runner作为JUnit测试无输出,Maven命令行执行正常

Eclipse中Karate Runner作为JUnit测试无输出的解决办法

在Eclipse中运行下方的Karate Runner文件作为JUnit测试时,控制台无任何输出,JUnit Explorer也没有显示执行记录,但使用mvn test命令行执行该文件则可正常运行。

问题代码

package com.accounting.apirunner;

import static org.junit.Assert.assertTrue;

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

import org.apache.commons.io.FileUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.intuit.karate.Results;
import com.intuit.karate.Runner;
import com.intuit.karate.junit5.Karate;

import cucumber.api.CucumberOptions;
import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportBuilder;

@CucumberOptions(features = { "classpath:Accountspayables", "classpath:Accountsreceivables", "classpath:company",
        "classpath:generalLedger", "classpath: SpendManagement" }, tags = {
                "~@ignore" }, plugin = { "pretty", "html:target/cucumber-html-reports" }, monochrome = true)

public class TestCaseRunner {

    @BeforeClass
    public static void beforeClass() throws Exception {

        System.setProperty("karate.env", "SAT");

    @Test
    public void testParallel() {

        Results results = Runner.path("classpath:Accountspayables", "classpath:Accountsreceivables",
                "classpath:company", "classpath:generalLedger", "classpath: SpendManagement").tags("~@ignore")
                .parallel(1);
        generateReport(results.getReportDir());
        assertTrue(results.getErrorMessages(), results.getFailCount() == 0);
        String karateOutputPath = "target/surefire-reports";
    }

    public static void generateReport(String karateOutputPath) {
        Collection<File> jsonFiles = FileUtils.listFiles(new File(karateOutputPath), new String[] { "json" }, true);
        List<String> jsonPaths = new ArrayList(jsonFiles.size());
        for (File file : jsonFiles) {
            jsonPaths.add(file.getAbsolutePath());
        }
        Configuration config = new Configuration(new File("target"), "Accounting XMLGW Regression Test Suite");
        ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config);
        reportBuilder.generateReports();
    }
}

问题截图

JUnit Explorer无执行记录截图


解决步骤

1. 修复代码语法错误

@BeforeClass方法缺少闭合大括号,会导致编译失败,Eclipse直接跳过测试执行。修正后:

@BeforeClass
public static void beforeClass() throws Exception {
    System.setProperty("karate.env", "SAT");
} // 补上缺失的大括号

2. 移除冲突依赖与注解

  • 代码同时引入了JUnit 4(org.junit.Test)和JUnit 5(com.intuit.karate.junit5.Karate),版本冲突会导致Eclipse的JUnit Runner识别异常。根据需求二选一:
    • 用JUnit 4:删除com.intuit.karate.junit5.Karate导入,确保项目依赖仅包含JUnit 4。
    • 用JUnit 5:替换@Test为@Karate.Test,改用JUnit 5的@BeforeAll替代@BeforeClass。
  • @CucumberOptions是Cucumber原生注解,和Karate的Runner API不兼容,直接删除该注解。

3. 修正路径空格问题

路径classpath: SpendManagement中SpendManagement前有空格,会导致Karate无法定位Feature文件,改为classpath:SpendManagement。

4. 调整Eclipse测试配置

  • 右键测试类 → Run As → Run Configurations,确认选择的JUnit版本(4或5)与代码依赖一致。
  • 右键项目 → Maven → Update Project,刷新依赖确保没有缺失或冲突。

5. 修正报告生成代码

代码中Collection&lt;File&gt;和List&lt;String&gt;是HTML转义字符,改为正常的<和>;同时new ArrayList(jsonFiles.size())需添加泛型<String>。


修正后的完整代码

package com.accounting.apirunner;

import static org.junit.Assert.assertTrue;

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

import org.apache.commons.io.FileUtils;
import org.junit.BeforeClass;
import org.junit.Test;

import com.intuit.karate.Results;
import com.intuit.karate.Runner;

import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportBuilder;

public class TestCaseRunner {

    @BeforeClass
    public static void beforeClass() throws Exception {
        System.setProperty("karate.env", "SAT");
    }

    @Test
    public void testParallel() {
        Results results = Runner.path("classpath:Accountspayables", "classpath:Accountsreceivables",
                "classpath:company", "classpath:generalLedger", "classpath:SpendManagement")
                .tags("~@ignore")
                .parallel(1);
        generateReport(results.getReportDir());
        assertTrue(results.getErrorMessages(), results.getFailCount() == 0);
    }

    public static void generateReport(String karateOutputPath) {
        Collection<File> jsonFiles = FileUtils.listFiles(new File(karateOutputPath), new String[] { "json" }, true);
        List<String> jsonPaths = new ArrayList<>(jsonFiles.size());
        for (File file : jsonFiles) {
            jsonPaths.add(file.getAbsolutePath());
        }
        Configuration config = new Configuration(new File("target"), "Accounting XMLGW Regression Test Suite");
        ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config);
        reportBuilder.generateReports();
    }
}

内容的提问来源于stack exchange,提问作者sateesh Reddy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 16:25:21