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

JUnit5+Surefire按标签执行Maven测试:命令行执行失败求助

问题解决思路

核心原因:测试类命名与Maven插件默认扫描范围不匹配

你的测试类命名为BubblegumApiIT,而maven-surefire-plugin默认只扫描执行命名符合**/*Test.java、**/*Tests.java、**/*TestCase.java的测试类;以IT结尾的测试类属于集成测试范畴,默认由maven-failsafe-plugin负责处理,所以mvn test命令根本不会扫描到这个类。

解决方法

方法1:用集成测试命令执行

直接调用集成测试相关命令,同时指定groups参数:

mvn -Dgroups=one verify

或者单独调用failsafe插件的目标:

mvn -Dgroups=one failsafe:integration-test

方法2:修改surefire插件的扫描规则

如果想继续用mvn test命令执行这个IT类,可在pom.xml的maven-surefire-plugin配置中添加扫描规则:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M5</version>
    <configuration>
        <groups>one</groups>
        <!-- 新增IT类的扫描规则 -->
        <includes>
            <include>**/*Test.java</include>
            <include>**/*IT.java</include>
        </includes>
    </configuration>
</plugin>

方法3:升级surefire插件版本

3.0.0-M5是早期预览版本,存在不少兼容性问题,建议升级到最新稳定版(比如3.2.5),新版本对JUnit 5的标签支持更完善:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.2.5</version>
    <configuration>
        <groups>one</groups>
    </configuration>
</plugin>

额外提醒:升级插件后,确保JUnit Jupiter依赖版本与插件兼容,比如:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.10.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.10.0</version>
    <scope>test</scope>
</dependency>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 10:27:10