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

基于Maven的Spring Boot项目打包可执行JAR运行Cucumber测试报错求助

问题根因

你遇到的报错核心是Cucumber在FAT JAR运行时没有扫描到带@CucumberContextConfiguration注解的配置类,具体有三个错误点:

  1. RunCucumberTest的main方法中调用Cucumber CLI的参数格式错误,没有指定--glue参数前缀,导致你传入的classpath:com.ing.testsuite被识别为Feature文件路径,而非胶水代码扫描路径,Cucumber根本没有扫描你的配置类所在的包。
  2. RunCucumberTest是包私有访问权限,且@SpringBootTest指定的配置类RunCucumberTest.class本身没有@Configuration注解,Spring无法正常加载上下文。
  3. assembly配置存在冗余的fileSet规则,虽然不直接导致报错,但可能引发资源加载异常。

修复方案

1. 修改RunCucumberTest.java

  • 将类改为public访问权限,新增@Configuration注解
  • 修正Cucumber CLI的调用参数,显式指定--glue前缀
    修改后代码如下:
package com.ing.testsuite;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;

@RunWith(Cucumber.class)
@CucumberOptions(features="src/test/resources", glue="com.ing.testsuite", plugin = {"pretty","html:target/cucumber.html"})
@Configuration
public class RunCucumberTest {

    public static void main(String[] args) throws Throwable {
        String[] arguments = {
                "--glue", "com.ing.testsuite",
                "classpath:dummy.feature",
                "--plugin", "pretty",
                "--plugin", "html:cucumber-report.html"
        };
        io.cucumber.core.cli.Main.main(arguments);
    }
}

2. 调整CucumberSpringConfiguration.java

将@SpringBootTest的classes指向正确的配置类,或者直接省略(SpringBoot会自动扫描当前包下的配置):

package com.ing.testsuite;

import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.boot.test.context.SpringBootTest;

@CucumberContextConfiguration
@SpringBootTest
public class CucumberSpringConfiguration{
}

3. 简化assembly.xml配置

删除冗余的feature文件单独配置规则,修改后配置如下:

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>fat-tests</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>test</scope>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/test-classes</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>**/*.*</include>
               </includes>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
    </fileSets>
</assembly>

4. 验证打包结果

打包完成后解压FAT JAR,确认以下两个资源存在:

  • com/ing/testsuite/CucumberSpringConfiguration.class
  • dummy.feature(在JAR根目录即可)

重新打包运行即可解决报错。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 20:45:05