当前位置: 首页 > news >正文

Spring Boot 单元测试什么时候需要添加 @RunWith

建立 Spring Boot 单元测试方法一般依赖于 JUnit4 或 JUnit5 框架。

在高版本的 Spring Boot 中,一般默认用的是 JUnit5。此时通过添加 @SpringBootTest 注解,即可成功注入相关的 bean 对象,并进行测试。

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class testClass {@Autowiredprivate LimitServiceImpl limitServiceImpl;@Testpublic void test() {}
}

如果使用的是 JUnit4,则需要额外添加 @Runwith(SpringRunner.class) 注解,用于声明测试的环境为 Spring 环境。

import org.junit.Test;
import org.junit.runner.Runwith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;@Runwith(SpringRunner.class)
@SpringBootTest
class testClass {@Autowiredprivate LimitServiceImpl limitServiceImpl;@Testpublic void test() {}
}

参考

  • 何时使用@RunWith
  • Spring Boot 单元测试基本使用,不污染数据库操作
  • Spring Boot 单元测试,通过 MockMvc 模拟 HTTP 请求
http://www.lryc.cn/news/403608.html

相关文章:

  • 鸿蒙OpenHarmony Native API【HiLog】
  • Pycharm 和虚拟环境的那些事?
  • rancher2里面的containerd的使用
  • Python数据风险案例54——人工智能热门概念股爬虫分析其价值(三因子模型)
  • 【HarmonyOS开发】Navigation使用
  • 计算机网络参考模型与5G协议
  • docker自建rustdesk-server远程桌面
  • 海外抖音黑屏是网络问题还是硬件问题?
  • 为了实现接口缓存,专门写了个缓存库 f-cache-memory
  • actual combat 35 —— es
  • android R ext4 image打包脚本介绍
  • 美式键盘 QWERTY 布局的来历
  • ETL数据同步之DataX,附赠一套DataX通用模板
  • [论文笔记] CT数据配比方法论——1、Motivation
  • 某4G区域终端有时驻留弱信号小区分析
  • 【体外诊断】ARM/X86+FPGA嵌入式计算机在免疫分析设备中的应用
  • Linux上启动和停止jar
  • 浏览器缓存:强缓存与协商缓存实现原理有哪些?
  • 持续集成04--Jenkins结合Gitee创建项目
  • 【Node.js基础02】fs、path模块
  • 牛客TOP101:单链表的排序
  • 数据可视化配色新工具,颜色盘多达2500+类
  • SpringAI简单使用(本地模型+自定义知识库)
  • 为什么要从C语言开始编程
  • [数据集][目标检测]导盲犬拐杖检测数据集VOC+YOLO格式4635张2类别
  • 数据结构(稀疏数组)
  • python 爬虫技术 第02节 基础复习
  • 数据结构-C语言-排序(3)
  • 【分布式事务】怎么解决分布式场景下数据一致性问题
  • C# 中的委托