junit5 入门系列教程-28-junit5 拓展实体-测试生命周期回调

时间:2025-03-27 09:27:12

目录

  • 目录
  • 测试生命周期回调
    • 接口
  • 开始和结束的回调
  • 系列导航

测试生命周期回调

接口

下面的接口定义了在测试执行生命周期的各个点上扩展测试的api。请参阅下面的小节,了解示例和Javadoc中的每个接口

参见

  • 实现多个扩展api

扩展开发人员可以选择在一个扩展中实现任意数量的这些接口。

有关具体示例,请参阅 SpringExtension 的源代码。

开始和结束的回调

BeforeTestExecutionCallback和AfterTestExecutionCallback定义了扩展的api,希望添加在测试方法执行之前和之后立即执行的行为。

因此,这些回调非常适合于计时、跟踪和类似的用例。如果您需要实现围绕@BeforeEach和@AfterEach方法调用的回调,那么应该实现BeforeEachCallback和AfterEachCallback。

下面的示例展示了如何使用这些回调来计算和记录测试方法的执行时间。TimingExtension既实现了BeforeTestExecutionCallback,又实现了AfterTestExecutionCallback,以便时间和记录测试执行。

import ;
import ;

import ;
import ;
import ;
import ;
import ;

public class TimingExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {

    private static final Logger logger = (());

    private static final String START_TIME = "start time";

    @Override
    public void beforeTestExecution(ExtensionContext context) throws Exception {
        getStore(context).put(START_TIME, ());
    }

    @Override
    public void afterTestExecution(ExtensionContext context) throws Exception {
        Method testMethod = ();
        long startTime = getStore(context).remove(START_TIME, long.class);
        long duration = () - startTime;

        (() -> ("Method [%s] took %s ms.", (), duration));
    }

    private Store getStore(ExtensionContext context) {
        return ((getClass(), ()));
    }
}

由于TimingExtensionTests类通过@ExtendWith注册了TimingExtension,它的测试将在执行时应用这个计时。

使用上述定义的类作为拓展

@ExtendWith()
class TimingExtensionTests {

    @Test
    void sleep20ms() throws Exception {
        (20);
    }

    @Test
    void sleep50ms() throws Exception {
        (50);
    }

}

日志输出如下:

INFO: Method [sleep20ms] took 24 ms.
INFO: Method [sleep50ms] took 53 ms.

系列导航

系列导航