行业资讯 2025年08月6日
0 收藏 0 点赞 902 浏览 3519 个字
摘要 :

文章目录 1.@RepeatedTest注解 1.1 语法 1.2 生命周期方法 2.自定义显示名称 3.RepetitionInfo接口 这段文本主要描述了JUnit框架中的@RepeatedTest注解,它用于编写……




  • 1.@RepeatedTest注解
    • 1.1 语法
    • 1.2 生命周期方法
  • 2.自定义显示名称
  • 3.RepetitionInfo接口

这段文本主要描述了JUnit框架中的@RepeatedTest注解,它用于编写可重复运行的测试模板。@RepeatedTest注解可以配置重复频率作为参数。

1.@RepeatedTest注解

@RepeatedTest注解用于标记需要重复特定次数并且可配置显示名称的测试方法。

如果想要用不同的参数重复测试,建议使用@ParameterizedTest注解。

1.1 语法

要创建可重复的测试,给测试方法添加@RepeatedTest注解。

在给定的示例中,测试方法使用了@RepeatedTest(5)注解,意味着该测试将执行五次。

@DisplayName(\"Add operation test\")
@RepeatedTest(5)
void addNumber(TestInfo testInfo) {
    Calculator calculator = new Calculator();
    Assertions.assertEquals(2, calculator.add(1, 1), \"1 + 1 should equal 2\");
}

1.2 生命周期方法

请注意,每次重复测试的行为都类似于常规测试的执行,并完全支持相同的生命周期回调和扩展。这意味着@BeforeEach和@AfterEach注解的生命周期方法将针对每次测试调用进行调用。

@RunWith(JUnitPlatform.class)
public class RepeatedTestExample {

    @BeforeAll
    public static void init(){
        System.out.println(\"Before All init() method called\");
    }

    @BeforeEach
    public void initEach(){
        System.out.println(\"Before Each initEach() method called\");
    }

    @DisplayName(\"Add operation test\")
    @RepeatedTest(5)
    void addNumber(TestInfo testInfo, RepetitionInfo repetitionInfo)
    {
        System.out.println(\"Running addNumber test -> \" + repetitionInfo.getCurrentRepetition());
        Assertions.assertEquals(2, Calculator.add(1, 1), \"1 + 1 should equal 2\");
    }

    @AfterEach
    public void cleanUpEach(){
        System.out.println(\"After Each cleanUpEach() method called\");
    }

    @AfterAll
    public static void cleanUp(){
        System.out.println(\"After All cleanUp() method called\");
    }
}

上述测试的输出:

Before All init() method called
Before Each initEach() method called
After Each cleanUpEach() method called
Before Each initEach() method called
Running addNumber test -> 1
After Each cleanUpEach() method called
Before Each initEach() method called
Running addNumber test -> 2
After Each cleanUpEach() method called
Before Each initEach() method called
Running addNumber test -> 3
After Each cleanUpEach() method called
Before Each initEach() method called
Running addNumber test -> 4
After Each cleanUpEach() method called
Before Each initEach() method called
Running addNumber test -> 5
After Each cleanUpEach() method called
After All cleanUp() method called

2.自定义显示名称

除了指定重复次数外,还可以为每个重复提供自定义显示名称,这个自定义显示名称可以是{static text + dynamic placeholders}的组合。

目前,支持三种占位符:

  • {displayName}:@RepeatedTest方法的显示名称。
  • {currentRepetition}:当前重复计数。
  • {totalRepetitions}:总的重复次数。
public class JUnit5AnnotationsExample
{
    @DisplayName(\"Add operation test\")
    @RepeatedTest(value = 5, name = \"{displayName} - repetition {currentRepetition} of {totalRepetitions}\")
    void addNumber(TestInfo testInfo) {
        Assertions.assertEquals(2, Calculator.add(1, 1), \"1 + 1 should equal 2\");
    }
}

运行上述测试将输出以下内容:JUnit 5 @RepeatedTest注解详解我们可以使用两种预定义格式之一,即LONG_DISPLAY_NAME和SHORT_DISPLAY_NAME。如果未指定,则默认使用后者。

  • RepeatedTest.LONG_DISPLAY_NAME – {displayName} :: repetition {currentRepetition} of {totalRepetitions}
  • RepeatedTest.SHORT_DISPLAY_NAME – repetition {currentRepetition} of {totalRepetitions}
@DisplayName(\"Add operation test\")
@RepeatedTest(value = 5, name = RepeatedTest.LONG_DISPLAY_NAME)
void addNumber(TestInfo testInfo) {
    Assertions.assertEquals(2, Calculator .add(1, 1), \"1 + 1 should equal 2\");
}

3.RepetitionInfo接口

RepetitionInfo用于获取当前重复测试在@RepeatedTest注解或生命周期方法(如@BeforeEach和@AfterEach)中的重复信息。

public class JUnit5AnnotationsExample {
    @BeforeEach
    public void initEach(RepetitionInfo info){
        int currentRepetition = info.getCurrentRepetition();
        int totalRepetitions = info.getTotalRepetitions();
        //Use information as needed
    }
    @DisplayName(\"Add operation test\")
    @RepeatedTest(value = 5, name=\"{displayName} :: repetition {currentRepetition} of {totalRepetitions}\")
    void addNumber(TestInfo testInfo) {
        Calculator calculator = new Calculator();
        Assertions.assertEquals(2, calculator.add(1, 1), \"1 + 1 should equal 2\");
    }
    @AfterEach
    public void cleanUpEach(RepetitionInfo info){
        int currentRepetition = info.getCurrentRepetition();
        int totalRepetitions = info.getTotalRepetitions();
        //Use information as needed
    }
}

 

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/9532.html

管理员

相关推荐
2025-08-06

文章目录 一、Reader 接口概述 1.1 什么是 Reader 接口? 1.2 Reader 与 InputStream 的区别 1.3 …

988
2025-08-06

文章目录 一、事件溯源 (一)核心概念 (二)Kafka与Golang的优势 (三)完整代码实现 二、命令…

465
2025-08-06

文章目录 一、证明GC期间执行native函数的线程仍在运行 二、native线程操作Java对象的影响及处理方…

348
2025-08-06

文章目录 一、事务基础概念 二、MyBatis事务管理机制 (一)JDBC原生事务管理(JdbcTransaction)…

456
2025-08-06

文章目录 一、SnowFlake算法核心原理 二、SnowFlake算法工作流程详解 三、SnowFlake算法的Java代码…

517
2025-08-06

文章目录 一、本地Jar包的加载操作 二、本地Class的加载方法 三、远程Jar包的加载方式 你知道Groo…

832
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号