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

文章目录 1.@AfterAll注解 2.@AfterAll注解示例 JUnit 5中的@AfterAll注解是JUnit 4中@AfterClass注解的替代品。它用于测试类的清理。 @AfterAll注解用于指示在当前测……




  • 1.@AfterAll注解
  • 2.@AfterAll注解示例

JUnit 5中的@AfterAll注解是JUnit 4中@AfterClass注解的替代品。它用于测试类的清理。

@AfterAll注解用于指示在当前测试类的所有测试之后执行带有@AfterAll注解的方法。

请注意,如果您想在每次测试后执行方法,可以使用@AfterEach注解。

1.@AfterAll注解

使用@AfterAll注解一个方法,如下所示:

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

请记住:

  • 带有@AfterAll注解的方法必须具有void返回类型,并且不能为private。
  • 带有@AfterAll注解的方法可以可选地声明要由ParameterResolvers解析的参数。
  • 从父类继承的@AfterAll方法只要不被隐藏或覆盖,就会一直继承下去。此外,父类中的@AfterAll方法将在子类中的@AfterAll方法之前执行。
  • 带有@AfterAll注解的方法必须是静态方法,否则将抛出运行时错误。
org.junit.platform.commons.JUnitException: @AfterAll method \'public void com.howtodoinjava.junit5.examples.JUnit5AnnotationsExample.cleanUp()\' must be static.
    at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.assertStatic(LifecycleMethodUtils.java:66)
    at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.lambda$findAfterAllMethods$1(LifecycleMethodUtils.java:48)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1080)
    at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findAfterAllMethods(LifecycleMethodUtils.java:48)

2.@AfterAll注解示例

让我们通过一个例子来说明。我们使用了Calculator类并添加了一个add方法。我们使用@RepeatedTest注解来测试add方法5次。但是,@AfterAll注解的方法只会被调用一次。

@RunWith(JUnitPlatform.class)
public class AfterAnnotationsTest {
 
    @DisplayName(\"Add operation test\")
    @RepeatedTest(5)
    void addNumber(TestInfo testInfo, RepetitionInfo repetitionInfo) 
    {
        System.out.println(\"Running test -> \" + repetitionInfo.getCurrentRepetition());
        Assertions.assertEquals(2, Calculator.add(1, 1), \"1 + 1 should equal 2\");
    }
     
    @AfterAll
    public static void cleanUp(){
        System.out.println(\"After All cleanUp() method called\");
    }
     
    @AfterEach
    public void cleanUpEach(){
        System.out.println(\"After Each cleanUpEach() method called\");
    }
}

其中Calculator类为:

public class Calculator
{
    public int add(int a, int b) {
        return a + b;
    }
}

运行测试,您将看到以下控制台输出:

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

显然,带有@AfterAll注解的cleanUp()方法只被调用一次。

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/9497.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

扫描二维码

关注微信客服号