JUnit 5学习笔记

1.JUnit5的改变

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

JUnit Platform: Junit
Platform是在JVM上启动测试框架的基础,不仅支持Junit自制的测试引擎,其他测试引擎也都可以接入。

JUnit Jupiter: JUnit Jupiter提供了JUnit5的新的编程模型,是JUnit5新特性的核心。内部包含了一个测试引擎,用于在Junit Platform上运行。

JUnit Vintage: 由于JUint已经发展多年,为了照顾老的项目,JUnit Vintage提供了兼容JUnit4.x,Junit3.x的测试引擎。

2.JUnit5常用注解及测试

JUnit5的注解与JUnit4的注解有所变化

  • @Test :表示方法是测试方法。但是与JUnit4的@Test不同,他的职责非常单一不能声明任何属性,拓展的测试将会由Jupiter提供额外测试
  • @ParameterizedTest :表示方法是参数化测试,下方会有详细介绍
  • @RepeatedTest :表示方法可重复执行,下方会有详细介绍
  • @DisplayName :为测试类或者测试方法设置展示名称
  • @BeforeEach :表示在每个单元测试之前执行
  • @AfterEach :表示在每个单元测试之后执行 @BeforeAll :表示在所有单元测试之前执行
  • @AfterAll :表示在所有单元测试之后执行
  • @Tag :表示单元测试类别,类似于JUnit4中的@Categories
  • @Disabled :表示测试类或测试方法不执行,类似于JUnit4中的@Ignore
  • @Timeout :表示测试方法运行如果超过了指定时间将会返回错误
  • @ExtendWith :为测试类或测试方法提供扩展类引用

2.1 @DisplayName/@Disabled/@BeforeEach/@AfterEach/@BeforeAll/@AfterAll

@SpringBootTest
@DisplayName("Junit5功能测试类")
public class TestJunit {
   
    //----------------------------常用注解----------------------------
//
    @DisplayName("测试DisplayName注解---方法1")
    @Test
    public void testDisplayName1() {
   
        System.out.println(123);
    }

    @Disabled
    @DisplayName("测试DisplayName注解---方法2")
    @Test
    public void testDisplayName2() {
   
        System.out.println(456);
    }

    @BeforeEach
    public void testBeforeEach() {
   
        System.out.println("测试就要开始了....");
    }

    @AfterEach
    public void testAfterEach() {
   
        System.out.println("测试已经结束了....");
    }

    @BeforeAll
    public static void testBeforeAll() {
   
        System.out.println("所有测试就要开始了....");
    }

    @AfterAll
    public static void testAfterAll() {
   
        System.out.println("所有测试已经结束了....");
    }
}

在这里插入图片描述

我们再把 testDisplayName2 方法上的@Disabled注解打开。可以看到,这个测试方法已经被禁用了。

在这里插入图片描述

2.2 @Timeout

package com.szh.boot;

import org.junit.jupiter.api.*;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@DisplayName("Junit5功能测试类")
public class TestJunit {
   
    @BeforeEach
    public void testBeforeEach() {
   
        System.out.println("测试就要开始了....");
    }

    @AfterEach
    public void testAfterEach() {
   
        System.out.println("测试已经结束了....");
    }

    @BeforeAll
    public static void testBeforeAll() {
   
        System.out.println("所有测试就要开始了....");
    }

    @AfterAll
    public static void testAfterAll() {
   
        System.out.println("所有测试已经结束了....");
    }

    @DisplayName("测试Timeout注解")
    @Timeout(value = 5, unit = TimeUnit.MILLISECONDS)
    @Test
    public void testTimeout() throws InterruptedException {
   
        Thread.sleep(1000);
    }
}

在这里插入图片描述

2.3 @RepeatedTest

package com.szh.boot;

import org.junit.jupiter.api.*;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@DisplayName("Junit5功能测试类")
public class TestJunit {
   
    @BeforeEach
    public void testBeforeEach() {
   
        System.out.println("测试就要开始了....");
    }

    @AfterEach
    public void testAfterEach() {
   
        System.out.println("测试已经结束了....");
    }

    @BeforeAll
    public static void testBeforeAll() {
   
        System.out.println("所有测试就要开始了....");
    }

    @AfterAll
    public static void testAfterAll() {
   
        System.out.println("所有测试已经结束了....");
    }

    @RepeatedTest(5)
    @Test
    public void testRepeatedTest() {
   
        System.out.println(1);
    }
}

在这里插入图片描述

3.断言

断言(assertions)是测试方法中的核心部分,用来对测试需要满足的条件进行验证。这些断言方法都是org.junit.jupiter.api.Assertions 的静态方法。JUnit 5 内置的断言可以分成如下几个类别:

3.1 简单断言

用来对单个值进行简单的验证。如:

方法 说明
assertEquals 判断两个对象或两个原始类型是否相等
assertNotEquals 判断两个对象或两个原始类型是否不相等
assertSame 判断两个对象引用是否指向同一个对象
assertNotSame 判断两个对象引用是否指向不同的对象
assertTrue 判断给定的布尔值是否为 true
assertFalse 判断给定的布尔值是否为 false
assertNull 判断给定的对象引用是否为 null
assertNotNull 判断给定的对象引用是否不为 null
package com.szh.boot;

import org.junit.jupiter.api.*;

import java.time.Duration;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值