码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • springboot--单元测试


    单元测试

    • 前言
    • 1、写测试要用的类
    • 2、写测试要用的类
    • 3、运行测试类
    • 4、spring-boot-starter-test默认提供了以下库
      • 4.1 junit5
        • 4.1.1 @DisplayName:为测试类或者测试方法设置展示名称
        • 4.1.2 @BeforeAll:所有测试方法运行之前先运行这个
        • 4.1.3 @BeforeEach:每个测试方法运行之前运行
      • 4.2 断言
        • 4.2.1 Assertions.assertEquals
        • 4.2.2嵌套测试
        • 4.2.3参数话测试 @ParameterizedTest

    前言

    Spring boot 提供一系列测试工具集及注释方便我们进行测试
    spring-boot-test提供核心测试能力,spring-boot-test-autoconfigure 提供测试的一些自动化配置
    我们只需导入spring-boot-starter-test即可整合测试

    在这里插入图片描述
    在这里插入图片描述

    1、写测试要用的类

    在这里插入图片描述

    package com.example.boot306demo.service;
    
    import org.springframework.stereotype.Service;
    
    /**
     * @author jitwxs
     * @date 2023年11月15日 20:27
     */
    @Service
    public class HelloService {
        public int sum(int a,int b){
            return a+b;
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    2、写测试要用的类

    自动注入任意组件即可测试
    在这里插入图片描述

    package com.example.boot306demo;
    
    import com.example.boot306demo.service.HelloService;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest  //具备测试springboot应用容器中所有组件的功能
    class Boot306DemoApplicationTests {
        @Autowired //自动注入任意组件即可测试
        HelloService helloService;
    
        @Test
        void contextLoads() {
            int sum = helloService.sum(1,2);
            System.out.println(sum);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3、运行测试类

    在这里插入图片描述

    4、spring-boot-starter-test默认提供了以下库

    junit5/spring Test/Assertj/Hamcrest/Mockito/JSONassert/jsonPath

    4.1 junit5

    官方英文文档为:https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations
    中文文档:https://gitee.com/liushide/junit5_cn_doc/blob/master/junit5UserGuide_zh_cn.md
    在这里插入图片描述

    4.1.1 @DisplayName:为测试类或者测试方法设置展示名称

    在这里插入图片描述

    4.1.2 @BeforeAll:所有测试方法运行之前先运行这个

    4.1.3 @BeforeEach:每个测试方法运行之前运行

    在这里插入图片描述

    4.2 断言

    Assertions
    在这里插入图片描述

    4.2.1 Assertions.assertEquals

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    4.2.2嵌套测试

    junit5 可以通过java中的内部类和@Nested注解实现嵌套测试,从而可以更好的把相关的测试方法组织在一起,在内部类中可以使用@BeforeEach和@AfterEach注解,而且嵌套的层次没有限制

    4.2.3参数话测试 @ParameterizedTest

    @ParameterizedTest //参数话测试
        @ValueSource(strings = {"one","two","three"})
        @DisplayName("参数话测试")
        public void parameterizedTest(String string){
            System.out.println(string);
            Assertions.assertTrue(StringUtils.isNotBlank(string));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    2.4_4死锁的处理策略---检测和解除
    使用SylixOS虚拟串口,实现系统串口自由
    MySQL join原理及优化
    Web前端开发技术课程大作业:基于HTML+CSS+JavaScript实现校园主题-萍乡田家炳中学校网站(1页)
    【运维】Linux修改Hosts
    【Linux基础】基础I/O(一)
    利用图像二维熵实现视频信号丢失检测(Signal Loss Detection)
    React 展开运算符
    Qt树形表格控件QTreeWidget的使用:添加自定义列表项
    【数据结构】顺序栈
  • 原文地址:https://blog.csdn.net/m0_50207524/article/details/134428575
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号