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

升级maven-surefire-plugin至3.1.2后测试执行及报告生成失败求助

Maven Surefire 3.1.2升级后Cucumber JSON报告生成失败的解决办法

问题

把maven-surefire-plugin从2.20版本升级到3.1.2后,执行mvn clean test时出现以下报错:

WARNING: Unexpected errornet.masterthought.cucumber.ValidationException: No JSON report file was found!at net.masterthought.cucumber.ReportParser.parseJsonFiles(ReportParser.java:61)at net.masterthought.cucumber.ReportBuilder.generateReports(ReportBuilder.java:97)

原2.20版本执行该命令能正常生成target/cucumber/cucumber.json文件,但升级后无法生成。升级插件是因为旧版本依赖的log4j存在安全漏洞,尝试添加junit-jupiter-plugin后问题仍未解决。

原pom.xml关键片段

<properties>
    <maven.compiler.source>18</maven.compiler.source>
    <maven.compiler.target>18</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
            <configuration>
                <source>18</source>
                <target>18</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.1.2</version>
            <configuration>
                <includes>
                    <include>utility.testRunnerClass</include>
                </includes>
                <testFailureIgnore>true</testFailureIgnore>
            </configuration>
        </plugin>

        <plugin>
            <groupId>net.masterthought</groupId>
            <artifactId>maven-cucumber-reporting</artifactId>
            <version>5.7.5</version>
            <executions>
                <execution>
                    <id>execution</id>
                    <phase>test</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <projectName>Automations</projectName>
                        <outputDirectory>target/cucumber/</outputDirectory>
                        <buildNumber>1</buildNumber>
                        <skip>false</skip>
                        <inputDirectory>${project.build.directory}/cucumber</inputDirectory>
                        <jsonFiles>
                            <param>**/*.json</param>
                        </jsonFiles>
                        <checkBuildResult>false</checkBuildResult>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

测试运行类代码

package utility;

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/java/features",
        glue = { "stepDefinitions", "utility" },
        tags = "@smoke",
        dryRun = false,
        publish = true,
        plugin = {"pretty", "html:target/cucumber/simple-reports.html",
        "json:target/cucumber/cucumber.json", "junit:target/cucumber/cucumber.xml"}
)
public class testRunnerClass {

}

原因分析

Surefire 3.x版本默认使用JUnit 5(Jupiter)作为测试执行器,但你的测试类使用的是JUnit 4的@RunWith(Cucumber.class)注解,导致Surefire无法正确识别并执行测试类,进而无法生成Cucumber的JSON报告。

解决步骤

1. 给Surefire指定JUnit 4执行器

修改maven-surefire-plugin的配置,明确指定使用JUnit 4执行测试,并添加对应的依赖支持:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.1.2</version>
    <configuration>
        <!-- 指定使用JUnit 4执行测试 -->
        <providerSelection>junit4</providerSelection>
        <includes>
            <include>utility.testRunnerClass</include>
        </includes>
        <testFailureIgnore>true</testFailureIgnore>
    </configuration>
    <!-- 添加JUnit 4的Surefire依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit4</artifactId>
            <version>3.1.2</version>
        </dependency>
    </dependencies>
</plugin>

2. 确保Cucumber依赖版本兼容

如果项目中Cucumber版本过旧,可能与Surefire 3.1.2不兼容,建议升级到7.x版本,在pom的dependencies节点中添加:

<dependencies>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>7.14.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>7.14.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

3. 修正报告输出路径(可选)

移除maven-cucumber-reporting配置中outputDirectory末尾的斜杠,避免路径解析问题:

<outputDirectory>target/cucumber</outputDirectory>

验证

修改配置后执行mvn clean test,检查target/cucumber目录下是否生成了cucumber.json、simple-reports.html和cucumber.xml文件,同时查看控制台输出,确认测试类正常执行、报告插件成功生成报告。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 08:40:01