JUnit5技术分享

简介: Spring Boot 2.2.0 版本开始引入Junit5 作为单元测试默认库最新的JUnit框架 与之前的有很大不同由三个不同子项目的几个不同模块组成Junit Platform (公共测试平台): 基础核心内容 ,不包括单元测试的一些测试引擎JUnit Junpiter(核心测试引擎): 是JUnit5 新特性的核心。内部包含了一个测试引擎,用于在JunitPlatform上运行JUnit Vintage:兼容JUnit4 JUnit3

一,JUnit5常用注解



https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations


1.@DisplayName


1.png


package com.yer.boot;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("junit5 功能测试类")
public class JUnit5Test {
    @DisplayName("测试displayname注解")
    @Test
    void testDisplayName(){
        System.out.println(999);
    }
}


2.@BeforeEach


@AfterEach


@BeforeAll


@AfterAll


package com.yer.boot;
import org.junit.jupiter.api.*;
@DisplayName("junit5 功能测试类")
public class JUnit5Test {
    @DisplayName("测试displayname注解")
    @Test
    void testDisplayName() {
        System.out.println(999);
    }
    @DisplayName("测试displayname注解2")
    @Test
    void testDisplayName2() {
        System.out.println(9992);
    }
    @BeforeEach
        //每个单元测试之前都要执行
    void testBeforeEach() {
        System.out.println("测试要开始了!");
    }
    @AfterEach
        //每个单元测试之后都要执行
    void testAfterEach() {
        System.out.println("测试结束了");
    }
    @BeforeAll
    static void testBeforeAll() {
        System.out.println("要测试所有测试了");
    }
    @AfterAll
    static void testAfterAll() {
        System.out.println("所有测试测试完了");
    }
}


3.@Tag 表示单元测试类别,类似于JUni4中的@Categories

4.@Disable 不用执行


2.png


5.@Timeout


/**
*
* 规定方法的超时时间
* 超出时间测试异常
* @throws InterruptedException
*/
@Timeout(value = 500,unit = TimeUnit.MILLISECONDS)
    @Test
    void testTimeout() throws InterruptedException {
        Thread.sleep(1000);
    }


6.@ExtendWIth:为测试类或测试方法提供扩展类引用 (类似于junit4@RunWith)


@SpringBootTest 复合注解中有@ExtendWIth(SpringExtension.class)


7.@RepeatTest(8) 重复测试


二,断言assertions



断言是测试方法中的核心部分,用来对测试需要满足的条件进行验证


这些断言方法都是org.junit.jupiter.api.Assertions中的静态方法


JUnit5 内置的断言可以分成以下几个类别:检查业务逻辑返回的数据是否合理


得益于断言机制,所有的测试结束之后会有一个详细报告


1.简单断言


对单个值进行简单验证


 @DisplayName("测试简单断言")
    @Test
    void testSimpleAssertions(){
        int cal = cal(3,3);
        assertEquals(6,cal);
    }
    int cal(int i,int j){
        return i+j;
    }
//成功 
=======================================
    /**
     *
     * 断言
     * 前面断言失败,后面代码不会执行
     */
    @DisplayName("测试简单断言")
    @Test
    void testSimpleAssertions(){
        int cal = cal(2,3);
        assertEquals(6,cal,"业务逻辑计算失败");
        Object o1 = new Object();
        Object o2 = new Object();
        assertEquals(o1,o2,"两个对象不一样");
    }
    int cal(int i,int j){
        return i+j;
    }


2.数组断言


来判断两个对象或者原始类型的数组是否相等


@Test
@DisplayName("array assertion")
public void array(){
    assertArrayEquals(new int[]{1,2},new int{1,2});
}


3.组合断言


assertAll方法接收多个org.junit.jupiter.api.Executable函数式接口的实例作为要验证的断言,可以通过lambda表达式 很容易的提供这些断言


 @Test
 @DisplayName("组合断言")
  void all(){
    assertAll("test",
              ()->assertTrue(true&&true,"结果不为true"),
              ()->assertEquals(1,1,"结果不为1"));
    system.out.println("两个断言都成功才可以输出")
  }


4.异常断言

在junit4时期 ,想要检测方法异常情况时,需要用注解@Rule注解的@ExpectedException变量还是比较麻烦的。相对于现在的Junit5提供了一种新的断言方式Assertions.assertThrows(),配合函数式编程就可以进行使用


断定业务逻辑一定出现异常


@Test
    @DisplayName("异常断言")
    void testException(){
        assertThrows(ArithmeticException.class,
                ()-> { int i = 1/0;},"业务逻辑竟然能正常运行,不是吧");
    }


5.超时断言


还提供了Assertions.assertTimout()为测试方法设置了超时时间


@Test
@DisplayName("超时断言")
public void timeOut(){
    //如果测试方法时间超过1s
    Assertions.assertTimeout(Duration.ofMillis(1000),
                             ()->Thread.sleep(500));
}

6.快速失败


通过fail方法直接使得测试失败


 

    @Test
    @DisplayName("快速失败")
    void testFai(){
        if (2 == 2 ){
            fail("测试失败");
        }
    }


三,前置条件(assumptions)



JUnit5中的前置条件(assunption假设)类似于断言,不同之处在于不满足的断言会使得测试方法失败,而不满足的前置条件只会使得测试方法执行终止。


前置条件爱你可以堪称是测试方法执行的前提,当该前提不满足时,就没有继续执行的必要l


 

    @DisplayName("测试前置条件")
    @Test
    void testAsummptions(){
        Assumptions.assumeTrue(false,"结果不是true");
        System.out.println("为true");
    }

disable 跳过


假设失败跳过


四,嵌套测试



-----------可参考官方文档


junit可以通过加入了嵌套测试


嵌套测试情况下


外层的Test不能驱动内层的Before(After)Each/All主类的方法提前/之后运行


内层的可以驱动外层的


package com.yer.boot;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.util.EmptyStackException;
import java.util.Stack;
import static org.junit.jupiter.api.Assertions.*;
@DisplayName("嵌套测试")
public class TestAStackDemo {
    Stack<Object> stack;
    @Test
    @DisplayName("new Stack()")
    void isInstantiatedWithNew() {
        new Stack<>();
    }
    @Nested
    @DisplayName("when new")
    class WhenNew {
        @BeforeEach
        void createNewStack() {
            stack = new Stack<>();
        }
        @Test
        @DisplayName("is empty")
        void isEmpty() {
            assertTrue(stack.isEmpty());
        }
        @Test
        @DisplayName("throws EmptyStackException when popped")
        void throwsExceptionWhenPopped() {
            assertThrows(EmptyStackException.class, stack::pop);
        }
        @Test
        @DisplayName("throws EmptyStackException when peeked")
        void throwsExceptionWhenPeeked() {
            assertThrows(EmptyStackException.class, stack::peek);
        }
        @Nested
        @DisplayName("after pushing an element")
        class AfterPushing {
            String anElement = "an element";
            @BeforeEach
            void pushAnElement() {
                stack.push(anElement);
            }
            @Test
            @DisplayName("it is no longer empty")
            void isNotEmpty() {
                assertFalse(stack.isEmpty());
            }
            @Test
            @DisplayName("returns the element when popped and is empty")
            void returnElementWhenPopped() {
                assertEquals(anElement, stack.pop());
                assertTrue(stack.isEmpty());
            }
            @Test
            @DisplayName("returns the element when peeked but remains not empty")
            void returnElementWhenPeeked() {
                assertEquals(anElement, stack.peek());
                assertFalse(stack.isEmpty());
            }
        }
    }
}


五,参数化测试



可参考官方文档


参数化测试是Junit5 很重要的一个新特性


它使得用不同的参数多次运行测试成为了可能,也为我们的单元测试带来许多便利。


利用**@ValueSource**等注解,指定入参,我们将可以使用不同的参数进行多次单元测试,而不需要每新增一个参数就新增一个单元测试,省去了很多冗余代码。


  • @ValueSource:为参数化测试指定入参来源,支持八大基础类 以及String类型Class类型


  • @NullSource表示为参数化测试提供一个null的入参@EnumSource表示为参数化测试提供一个枚举入参


  • @CsvFileSource:表示读取指定CSV文件内容作为参数化测试入参


  • @MethodSource:表示读取指定方法的返回值作为参数化测试入参(注意方法返回需要是一个流)


parameterized


 @ParameterizedTest
    @DisplayName("参数化测试")
    @ValueSource(ints = {1,2,3,4,5})
    void  testParameterized(int i){
        System.out.println(i);
    }
    //五个参数同时传进来的效果  把所有要测的统一测一遍
@ParameterizedTest
    @DisplayName("参数化测试")
    @MethodSource("stringProvider")
    void  testParameterized2(String i){
        System.out.println(i);
    }
    static Stream<String> stringProvider() {
        return Stream.of("apple", "banana","我最喜欢的大樱桃");
    }

junit4中的一些不可用了当你在junit4迁移过来的时候记得要看啊!


https://junit.org/junit5/docs/current/user-guide/#migrating-from-junit4


  • Annotations reside in the org.junit.jupiter.api package.


  • Assertions reside in org.junit.jupiter.api.Assertions.


  • Note that you may continue to use assertion methods from org.junit.Assert or any other assertion library such as AssertJ, Hamcrest, Truth, etc.
  • Assumptions reside in org.junit.jupiter.api.Assumptions.


  • Note that JUnit Jupiter 5.4 and later versions support methods from JUnit 4’s org.junit.Assume class for assumptions. Specifically, JUnit Jupiter supports JUnit 4’s AssumptionViolatedException to signal that a test should be aborted instead of marked as a failure.
  • @Before and @After no longer exist; use @BeforeEach and @AfterEach instead.


  • @BeforeClass and @AfterClass no longer exist; use @BeforeAll and @AfterAll instead.


  • @Ignore no longer exists: use @Disabled or one of the other built-in execution conditions instead


  • @Category no longer exists; use @Tag instead.


  • @RunWith no longer exists; superseded by @ExtendWith.


  • @Rule and @ClassRule no longer exist; superseded by @ExtendWith and @RegisterExtension


stead.


  • @BeforeClass and @AfterClass no longer exist; use @BeforeAll and @AfterAll instead.


  • @Ignore no longer exists: use @Disabled or one of the other built-in execution conditions instead


  • @Category no longer exists; use @Tag instead.


  • @RunWith no longer exists; superseded by @ExtendWith.


  • @Rule and @ClassRule no longer exist; superseded by @ExtendWith and @RegisterExtension


相关文章
|
消息中间件 Cloud Native Java
解决Spring Boot 2.7.16 在服务器显示启动成功无法访问问题:从本地到服务器的部署坑
解决Spring Boot 2.7.16 在服务器显示启动成功无法访问问题:从本地到服务器的部署坑
1804 0
|
JavaScript Java 关系型数据库
毕设项目-基于Springboot和Vue实现蛋糕商城系统(一)
毕设项目-基于Springboot和Vue实现蛋糕商城系统
767 0
|
存储 JSON 前端开发
前端下载文件以及上传图片预览,顺便了解arrayBuffer和blob
前端下载文件以及上传图片预览,顺便了解arrayBuffer和blob
1751 0
|
10月前
|
安全 Java 开发工具
工程搭建与验证
本文介绍如何使用阿里云脚手架快速搭建Spring Boot工程,并整合Spring Security实现基础安全控制。包含工程创建、代码导入、Web依赖配置、访问接口开发及Security默认登录验证,附完整代码仓库地址。
|
7月前
|
人工智能 Linux API
【最新】OpenClaw保姆级图文教程:阿里云/本地部署步骤+百炼API配置+常见问题解答
作为小微企业主,日常经营中总会被客户档案整理、销售数据统计、库存提醒等重复性工作占据大量时间,忙碌之余还容易因琐事遗漏影响业务推进。而OpenClaw这款AI自动化助手,能将各类机械性的工作流程实现自动化处理,从文件整理、数据统计到定时提醒、消息发送,仅需简单的自然语言指令,就能让AI完成相应操作,大幅释放人工精力。本文将结合实际使用场景,详细讲解OpenClaw的功能应用,同时带来2026年新手零基础下,阿里云、MacOS、Linux、Windows11多平台的本地部署步骤,以及阿里云百炼API的配置方法和部署使用中的常见问题解答,让零基础用户也能轻松上手这款AI自动化工具。
3404 4
|
12月前
|
负载均衡 Java API
grpc-java 架构学习指南
本指南系统解析 grpc-java 架构,涵盖分层设计、核心流程与源码结构,结合实战路径与调试技巧,助你从入门到精通,掌握高性能 RPC 开发精髓。
1051 8
|
人工智能 搜索推荐 前端开发
打造个性化跨年时钟:整点报时与祝福语、背景音乐播放的完美结合
这款跨年时钟应用程序为2025新年特别设计,基于Python和Tkinter库开发。它不仅实时显示当前时间,还根据时间段自动调整背景颜色(白天白色、夜晚紫色),并在特定时间播放报时和祝福语音。指针颜色每隔6到15秒随机变化,增加趣味性。元旦前三秒显示“元旦快乐”并播放AI生成的祝福语音。窗口始终置顶,方便查看。此外,整点和半点时播放报时声音,背景音乐在指定时间启动,带来个性化体验。 代码实现包括环境准备、时区设置、动态背景和指针绘制、声音播放及主窗口创建等功能模块。通过多线程技术确保流畅运行。希望这款时钟能为新年增添温馨与惊喜。 欢迎点赞、关注、转发、收藏!
629 44
|
安全 Java 数据安全/隐私保护
Stirling-PDF:一款优秀的开源PDF处理工具
Stirling-PDF是一个基于spring-boot开发的开源项目,旨在提供一个功能强大的基于Docker的本地托管PDF操作工具。它使您能够对PDF文件进行多种操作,包括拆分、合并、转换、重新组织、添加图片、旋转、压缩等。该本地托管应用最初由ChatGPT完全开发,并已发展成一个功能齐全的工具,可满足您的各种PDF需求。
7381 3
|
编解码 安全 算法
Java多线程基础-18:线程安全的集合类与ConcurrentHashMap
如果这些单线程中的集合类确实需要在多线程中使用,该怎么办呢?思路有两个: 最直接的方式:使用锁,手动保证。如多个线程修改ArrayList对象,此时就可能有问题,就可以给修改操作进行加锁。但手动加锁的方式并不是很方便,因此标准库还提供了一些线程安全的集合类。
1127 4
|
缓存 网络协议 安全
【常见开源库的二次开发】HTTP之libcurl库——基础知识扫盲(一)
【常见开源库的二次开发】HTTP之libcurl库——基础知识扫盲(一)
910 1

热门文章

最新文章