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

如何配置maven-failsafe-plugin仅执行@WebserviceTest标注的测试用例

实现方案

要让自定义@WebserviceTest注解直接被maven-failsafe-plugin识别为webservice分组测试,按以下两步配置即可:

1. 补全自定义注解的元注解

你当前定义的注解缺少运行时保留、作用目标声明,JUnit运行时无法读取到注解上标记的@Tag元数据,修改注解代码如下:

import org.junit.jupiter.api.Tag;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Tag("webservice")
public @interface WebserviceTest {
    boolean executeAndIgnoreCondition() default false;
}

关键配置说明:

  • 必须添加@Retention(RetentionPolicy.RUNTIME),否则注解在测试运行阶段会被JVM丢弃,JUnit无法扫描到该注解及关联的@Tag
  • @Target声明注解可作用于类、测试方法,和原生@Tag的适用范围对齐

2. 修正maven-failsafe-plugin配置

你现有配置存在两个问题:

  • includes规则使用了单元测试的命名匹配规则**/Test*.java,这是maven-surefire-plugin的默认匹配规则,failsafe默认匹配的集成测试类命名为**/IT*.java、**/*IT.java、**/*ITCase.java,错误的规则会导致扫描范围异常
  • 插件版本需使用2.22.2及以上版本,低版本存在JUnit5元注解标签识别的bug
    修正后的配置参考:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>3.2.5</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <groups>webservice</groups>
    </configuration>
</plugin>

配置完成后,直接在测试类或测试方法上标注@WebserviceTest即可被识别为webservice分组测试,无需额外添加原生@Tag("webservice")注解。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 09:15:44