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

文章目录 1.@BeforeEach的使用方法 2.@BeforeEach示例 在JUnit 5中,@BeforeEach注解被用于表示当前类中的@Test、@RepeatedTest、@ParameterizedTest或@TestFactory方……




  • 1.@BeforeEach的使用方法
  • 2.@BeforeEach示例

在JUnit 5中,@BeforeEach注解被用于表示当前类中的@Test、@RepeatedTest、@ParameterizedTest或@TestFactory方法每次调用之前都应执行被注解的方法。@BeforeEach注解是JUnit 5中的测试生命周期方法之

一,用于替换JUnit 4中的@Before注解。默认情况下,测试方法将与@BeforeEach注解的方法在同一个线程中执行。

1.@BeforeEach的使用方法

1)在方法上添加@BeforeEach注解,如下所示:

@BeforeEach
public void initEach(){
     //test setup code
}

@Test
void succeedingTest() {
    //test code and assertions
}

2)@BeforeEach带注释的方法不能是静态方法,否则会抛出运行时错误。

@BeforeEach
public static void initEach(){
     //test setup code
}

//Error


org.junit.platform.commons.JUnitException: @BeforeEach method \'public static void com.howtodoinjava.junit5.examples. JUnit5AnnotationsExample.initEach()\' must not be static.
at org.junit.jupiter.engine.descriptor. LifecycleMethodUtils.assertNonStatic(LifecycleMethodUtils.java:73)

需要注意的是,如果父类(或接口)中存在@BeforeEach注解的方法,它将被子类继承,只要该方法没有被隐藏或覆盖。另外,父类(或接口)中的@BeforeEach注解的方法将在子类中的方法之前执行。

2.@BeforeEach示例

我们将使用Calculator类并添加一个add()方法进行测试。我们将使用@RepeatedTest注解测试该方法5次,这将导致add()测试方法运行5次。@BeforeEach带注释的方法应该在每次调用测试方法时执行。

public class BeforeEachTest {

    @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\");
    }

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

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

其中Calculator类如下所示:

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

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

BeforeAll init() method called
BeforeEach initEach() method called

BeforeEach initEach() method called
Running test -> 1

BeforeEach initEach() method called
Running test -> 2

BeforeEach initEach() method called
Running test -> 3

BeforeEach initEach() method called
Running test -> 4

BeforeEach initEach() method called
Running test -> 5

显然,@BeforeEach注解的initEach()方法每次测试方法调用时都会被调用一次。

微信扫一扫

支付宝扫一扫

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

扫描二维码

关注微信客服号