Junit5基础教程

本文详细介绍了Junit5的基础知识,包括单元测试的好处,Junit5的注解,特别是AssertJ的流式断言,以及如何使用MockMvc测试控制器。通过MockMvcRequestBuilders和ResultActions等API,可以模拟HTTP请求并验证控制器的行为。
摘要由CSDN通过智能技术生成

一、概述

单元测试的优点
  1. 提升软件质量
  2. 促进代码优化
单元测试的基本原则
  1. 单元测试要符合AIR原则
  • A : Automatic (自动化)
  • I : Independent (独立性)
  • R : Repeatable (可重复)
  1. 单元测试的代码层面要符合BCDE原则
  • B: Border 边界值测试,包括循环边界、特殊取值、特殊时间点、数据顺序等
  • C: Correct 正确的输入,并得到预期的结果
  • D: Design 与设计文档相结合,来编写单元测试
  • E : Error 单元测试的目标是证明程序有错,而不是程序无错。为了发现代码中潜在的错误,我们需要在编写测试用例时有一些强制的错误输入(如非法数据、异常流程、非业务允许输入等)来得到预期的错误结果

二、Junit5注解

注解 用法说明
@Test 表明方法是一个测试方法
@DisplayName 为测试类或者测试方法自定义一个名称
@BeforeAll 在所有测试方法运行前运行,并且只能修饰静态方法(除非修改测试实例生命周期)
@BeforeEach 每个测试方法运行前运行
@AfterEach 每个测试方法运行完毕后运行
@AfterAll 在所有测试方法运行完毕后运行
@Disabled 这个测试不会运行
@RepeatedTest 重复测试
@TestMethodOrder 指定测试顺序排序方式
@Order 注解定义测试顺序
package com.flamingo.junit5test;

import com.flamingo.junit5test.util.Add;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/**
 * 常用注解测试
 * @author flamingo
 * @create 7/6/21 11:04 PM
 */
@DisplayName("Common annotation test")
public class AnnotationsTest {
   
    private static Add add;

    @BeforeAll
    public static void beforeAll() {
   
        add = new Add();
        System.out.println("Run before all test methods run");
    }

    @BeforeEach
    public void beforeEach() {
   
        System.out.println("Run before each test method runs");
    }

    @AfterEach
    public void afterEach() {
   
        System.out.println("Run after each test method finishes running");
    }

    @AfterAll
    public static void afterAll() {
   
        System.out.println("Run after all test methods have finished running");
    }

    @Disabled
    @Test
    @DisplayName("Ignore the test")
    public void disabledTest() {
   
        System.out.println("This test will not run");
    }

    @Test
    @DisplayName("Test Methods 1+1")
    public void testAdd1() {
   
        System.out.println("Running test method1+1");
        Assertions.assertEquals(2, add.add(1, 1));
    }

    @Test
    @DisplayName("Test Methods 2+2")
    public void testAdd2() {
   
        System.out.println("Running test method2+2");
        Assertions.assertEquals(4,add.add(2,2));
    }
}

三、断言

常用断言 断言说明
assertEquals 断言预期值和实际值相等
assertAll 分组断言,执行其中包含的所有断言
assertArrayEquals 断言预期数组和实际数组相等
assertFalse 断言条件为假
assertNotNull 断言不为空|
assertSame 断言两个对象相等
assertTimeout 断言超时
assertThrows 断言异常,抛出指定的异常,测试才会通过
assertTimeoutPreemptively 断言超时,如果在定时内任务没有执行完毕,会立即返回断言失败
package com.flamingo.junit5test;

import org.junit.jupiter.api.Test;

import static java.time.Duration.ofMillis;
import static java.time.Duration.ofMinutes;
import static org.junit.jupiter.api.Assertions.*;

/**
 * @author flamingo
 * @create 7/6/21 11:19 PM
 */
public class Assert {
   
    @Test
    void standardAssertions() {
   
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值