JUnit5学习笔记

JUnit5

前言

版本说明

junit5=5.7.0

相关链接:

源码地址

  • Github:
  • Gitee:

JUnit5 介绍

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

  • JUnit Platform: 可作为一个基础发射测试框架在JVM上。它还定义了TestEngine用于开发在平台上运行的测试框架的API。
  • JUnit Jupiter: 是新的编程模型和 扩展模型的组合,用于在JUnit 5中编写测试和扩展。Jupiter子项目提供了一个TestEngine在平台上运行基于Jupiter的测试的功能。
  • JUnit Vintage: 提供了一个TestEngine在平台上运行基于JUnit 3和JUnit 4的测试的功能。

POM核心依赖

JUnit Jupiter 聚合器工件,可传递性地获取 junit-jupiter-apijunit-jupiter-paramsjunit-jupiter-engine的依赖关系,并在诸如 GradleMaven 之类的构建工具中简化了依赖关系管理。

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.7.0</version>
    <scope>test</scope>
</dependency>

常用注解

注解说明
@BeforeAll必须要是static方法。 只执行一次,执行时机是在所有测试和 @BeforeEach 注解方法之前
@BeforeEach在每个测试执行之前执行
@Test测试方法
@AfterEach在每个测试执行之后执行。
@AfterAll必须要是static方法。 只执行一次,执行时机是在所有测试和 @AfterEach 注解方法之后。
@RepeatedTest(value = N)重复N次执行测试
@DisplayName(“测试显示名”)声明测试类或测试方法的自定义显示名称
@Disabled用于禁用测试类或测试方法
@Nested表示带注释的类是一个非静态的嵌套测试类
@TestMethodOrder测试类排序方式:
DisplayName:根据测试方法的显示名称按字母顺序对它们进行排序;
OrderAnnotation:根据@Order值排序

实践指南

基本示例

package top.simba1949;

import org.junit.jupiter.api.*;

/**
 * @author SIMBA1949
 * @date 2020/10/17 19:06
 */
public class AppOneTest {

    @BeforeAll
    public static void beforeAll(){
        System.out.println("beforeAll");
    }
    @BeforeEach
    public void beforeEach(){
        System.out.println("beforeEach");
    }
    @Test
    public void testCore(){
        System.out.println("testCore");
    }
    @AfterEach
    public void afterEach(){
        System.out.println("afterEach");
    }
    @AfterAll
    public static void afterAll(){
        System.out.println("afterAll");
    }
}

输出结果

@RepeatedTest 示例

@RepeatedTest(value = 1) :value 表示再重复次数

package top.simba1949;

import org.junit.jupiter.api.*;

/**
 * @author SIMBA1949
 * @date 2020/10/17 19:10
 */
public class AppTwoTest {

    @BeforeAll
    public static void beforeAll(){
        System.out.println("beforeAll");
    }
    @BeforeEach
    public void beforeEach(){
        System.out.println("beforeEach");
    }
    @RepeatedTest(value = 1)
    @Test
    public void testCore(){
        System.out.println("testCore");
    }
    @AfterEach
    public void afterEach(){
        System.out.println("afterEach");
    }
    @AfterAll
    public static void afterAll(){
        System.out.println("afterAll");
    }
}

在这里插入图片描述

@DisplayName

package top.simba1949;

import org.junit.jupiter.api.*;

/**
 * @author SIMBA1949
 * @date 2020/10/17 19:15
 */
@DisplayName(value = "DisplayName-AppThreeTest")
public class AppThreeTest {
    @BeforeAll
    public static void beforeAll(){
        System.out.println("beforeAll");
    }
    @BeforeEach
    public void beforeEach(){
        System.out.println("beforeEach");
    }
    @Test
    @DisplayName(value = "DisplayName-testCore")
    public void testCore(){
        System.out.println("testCore");
    }
    @AfterEach
    public void afterEach(){
        System.out.println("afterEach");
    }
    @AfterAll
    public static void afterAll(){
        System.out.println("afterAll");
    }
}

未添加 @DisplayName 注解前,打印如下 :
在这里插入图片描述

添加 @DisplayName 注解后,打印如下:
在这里插入图片描述

@Disabled

@Disabled 是禁用某个测试类或者方法

package top.simba1949;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

/**
 * @author SIMBA1949
 * @date 2020/10/17 19:23
 */
public class AppFourTest {

    @Test
    @Disabled
    public void testMethodOne(){
        System.out.println("testMethodOne");
    }

    @Test
    public void testMethodTwo(){
        System.out.println("testMethodTwo");
    }
}

在这里插入图片描述

@Nested

@Nested 表示带注释的类是一个非静态的嵌套测试类

package top.simba1949;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

/**
 * @author SIMBA1949
 * @date 2020/10/17 19:27
 */
public class AppFiveTest {

    @Nested
    class AppNestedFiveTest{

        @Test
        public void appNestedFiveTestMethod(){
            System.out.println("appNestedFiveTestMethod");
        }
    }
}

@TestMethodOrder

@TestMethodOrder 测试类排序方式注解,常用排序方式:

  • DisplayName:根据测试方法的显示名称按字母顺序对它们进行排序;
  • OrderAnnotation:根据 @Order 值排序, @Order 整数,数值越小越先知心;
package top.simba1949;

import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

/**
 * @author SIMBA1949
 * @date 2020/10/17 19:29
 */
@TestMethodOrder(value = MethodOrderer.OrderAnnotation.class)
public class AppSixTest {

    @Test
    @Order(-2)
    public void methodA(){
        System.out.println("methodA");
    }

    @Test
    @Order(-10)
    public void methodB(){
        System.out.println("methodB");
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Simba1949

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值