junit5 入门系列教程-30-junit5 实战例子 junit performance

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

目录

  • 目录
  • 实战项目
    • junitperf
  • junit5 的新特性
    • 注解定义
    • 使用
    • 代码地址
  • 系列导航

实战项目

本系列的学习也正是为了将原来的项目,从 junit4 升级到 junit5

junitperf

Java 性能测试框架工具-JunitPerf

junit5 的新特性

注解定义

ps: 为了简化说明,删除了其他的属性。

import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;
import ;
import ;

/**
 * 执行接口
 * 对于每一个测试方法的条件配置
 * @author bbhou
 * @version 1.0.0
 * @since 1.0.0
 */
@Retention()
@Target({ElementType.ANNOTATION_TYPE, })
@Documented
@ExtendWith()
@TestTemplate
public @interface JunitPerfConfig {
    /**
     * 执行时间。(单位:毫秒)
     * 默认值:默认为 1min
     * 这里的执行时间不包含准备时间。
     * @return time in mills
     */
    long duration() default 60_000L;
}

使用

这样,就可以直接执行该测试方法。不再需要其他额外的注解。
将执行 @ExtendWith()中对应的实现。

import ;

public class HelloWorldTest {

    @JunitPerfConfig(duration = 1000)
    public void helloTest() throws InterruptedException {
        (100);
        ("Hello Junit5");
    }

}

代码地址

有兴趣的参见 junitperf

系列导航

系列导航