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

如何用JUnit5、Cucumber创建测试运行类并通过Maven Shade的Jar运行测试

JUnit4+io.cukes 迁移到 JUnit5+io.cucumber:创建测试运行类

迁移背景

当前正从JUnit4 + io.cukes技术栈迁移到JUnit5 + io.cucumber栈,现有流程是执行mvn clean package生成可执行Jar包,再通过java -jar "-Dcucumber.options...."命令运行测试,主类已在POM文件中配置。但遇到两个核心问题:

  • 找不到JUnit5中替代JUnitCore.main(className)的测试启动方法
  • 新版Cucumber不再支持@CucumberOptions和@RunWith注解

旧版代码参考

旧RunCukes.java

import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import managers.AutUtilities;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = {"./features"},  
        glue={"mercurysd"},
        plugin = {"pretty","html:target/cucumber-reports", "json:target/cucumber-reports/report.json"}
)

public class RunCukes {
    
    public static void main(String[] args) {
        AutUtilities.call().setLogging();
        JUnitCore.main(RunCukes.class.getName());
    }
}

旧POM中maven-shade-plugin配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
        <!-- Attach the shade goal into the package phase -->
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer
                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>mercurypo.RunCukes</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

新版实现方案

1. 新RunCukes.java代码

新版Cucumber用@Cucumber注解(来自io.cucumber.junit.platform.engine.Cucumber)替代旧的@RunWith和@CucumberOptions,同时用JUnit Platform的Launcher API实现测试启动:

import io.cucumber.junit.platform.engine.Cucumber;
import managers.AutUtilities;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.launcher.listeners.TestExecutionSummary;

import java.io.PrintWriter;

@Cucumber(
        features = "./features",
        glue = "mercurysd",
        plugin = {"pretty", "html:target/cucumber-reports", "json:target/cucumber-reports/report.json"}
)
public class RunCukes {

    public static void main(String[] args) {
        // 保留原有初始化逻辑
        AutUtilities.call().setLogging();

        // JUnit5测试启动逻辑
        SummaryGeneratingListener listener = new SummaryGeneratingListener();

        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                .selectors(org.junit.platform.engine.discovery.ClassNameFilter.includeClassNamePatterns(RunCukes.class.getName()))
                .build();

        Launcher launcher = LauncherFactory.create();
        launcher.registerTestExecutionListeners(listener);
        launcher.execute(request);

        // 打印测试执行摘要
        TestExecutionSummary summary = listener.getSummary();
        summary.printTo(new PrintWriter(System.out));

        // 根据测试结果设置退出码
        System.exit(summary.getTotalFailureCount() > 0 ? 1 : 0);
    }
}

2. 调整POM的maven-shade-plugin配置

需要添加资源转换器,确保JUnit Platform能发现Cucumber测试引擎(否则运行时会提示找不到测试):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <!-- 主类配置 -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>mercurypo.RunCukes</mainClass>
                    </transformer>
                    <!-- 合并JUnit Platform的服务文件 -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

3. 运行方式

保留原有流程:

  1. 执行mvn clean package生成可执行Jar
  2. 通过java -jar your-jar-name.jar "-Dcucumber.options=..."命令运行,额外的Cucumber参数依然可以通过系统属性传递

关键说明

  • @Cucumber注解:新版Cucumber整合JUnit5后,用该注解替代旧的@RunWith和@CucumberOptions,配置参数逻辑和旧版一致
  • JUnit Platform Launcher:替代JUnitCore.main,负责扫描和执行测试,同时可以获取测试执行摘要,方便输出结果和设置退出码
  • ServicesResourceTransformer:必须添加,否则Shade打包后会丢失测试引擎的服务注册信息,导致JUnit5无法识别Cucumber测试

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 22:10:10