【第8章】SpringBoot之单元测试

ops/2024/10/21 12:01:30/

文章目录

  • 前言
  • 一、准备
    • 1. 引入库
    • 2. 目录结构
  • 二、测试代码
    • 1. SpringBoot3ApplicationTests
    • 2.测试结果
  • 总结


前言

单元测试是SpringBoot项目的一大利器,在SpringBoot我们可以很轻松地测试我们的接口。


一、准备

1. 引入库

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>

2. 目录结构

通过IDEA新建SpringBoot项目已经包含了测试的目录

在这里插入图片描述

二、测试代码

我们前面刚刚整合了mybatis-plus,这里我们编写单元测试代码,跑一下

1. SpringBoot3ApplicationTests

代码如下(示例):

java">package org.example.springboot3;import org.example.springboot3.mybatis.controller.UserController;
import org.example.springboot3.mybatis.model.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;@SpringBootTest
class SpringBoot3ApplicationTests {@AutowiredUserController userController;@Testvoid contextLoads() {List<User> list = userController.mybatis002();list.stream().forEach(System.out::println);}}

2.测试结果

User(id=1, name=张三, age=11, brithDay=Mon May 19 00:00:00 CST 2014)
User(id=2, name=李四, age=10, brithDay=Tue May 19 00:00:00 CST 2015)

总结

回到顶部
一定要测试!一定要测试!一定要测试!

功夫再好,也怕菜刀!


http://www.ppmy.cn/ops/45262.html

相关文章

【Spring】使用 @Schedule 完成定时任务

在Spring框架中&#xff0c;使用Spring Schedule可以很方便地创建定时任务。以下是一个使用Spring Schedule完成定时任务的DEMO&#xff1a; 引入Spring Boot依赖&#xff1a;在pom.xml文件中添加Spring Boot Starter依赖&#xff0c;这会自动包含Spring Scheduling。 <de…

接口性能测试复盘:解决JMeter超时问题的实践

在优化接口并重新投入市场后&#xff0c;我们面临着一项关键任务&#xff1a;确保其在高压环境下稳定运行。于是&#xff0c;我们启动了一轮针对该接口的性能压力测试&#xff0c;利用JMeter工具模拟高负载场景。然而&#xff0c;在测试进行约一分钟之后&#xff0c;频繁出现了…

MVC和Filter

目录 MVC和三层架构模型的联系 Filter 概念 作用 应用场景 步骤 简单入门 注解开发 Filter过滤器的生命周期 Filter的拦截路径 过滤链 MVC和三层架构模型的联系 m-->model即模型是三层架构模型的业务层&#xff08;service&#xff09;和持久层(dao) v-->view…

基于Vue2与3版本的Element UI与Element Plus入门

基于Vue2与3版本的Element UI与Element Plus入门 Element UI 入门安装引入 Element UI使用组件 Element Plus 入门安装引入 Element Plus使用组件 常用组件自定义主题兼容性和升级社区和支持 Element UI 入门 Element UI 是基于 Vue 2.0 的桌面端组件库&#xff0c;它提供了一…

【算法】决策单调性优化DP

文章目录 决策单调性四边形不等式决策单调性 形式1法1 分治法2 二分队列例题 P3515Solution 形式2例题 P3195Solution 形式3例题 CF833BSolution 形式4例题Solution 后话 决策单调性 四边形不等式 定义: 对于二元函数 w ( x , y ) w(x,y) w(x,y)&#xff0c;若 ∀ a , b , …

Windows驱动开发系列文章一

文章目录 环境搭建如何调试实时调试非实时调试 环境搭建 基本上按照官方网站安装 VisualStudio/SDK/WDK 这些软件就可以了 详情请参考这个安装链接 如何调试 Windows 调试分为两种&#xff1a;一种是实时调试&#xff0c;一种是非实时调试 实时调试 这个就需要用到Microso…

React 使用JSX或者TSX渲染页面

02 Rendering with JSX Your first JSX content In this section, we’ll implement the obligatory " Hello, World " JSX application. At this point, we’re just dipping our toes in the water; more in-depth examples will follow. We’ll also discuss wh…

[双指针] --- 快乐数 盛最多水的容器

Welcome to 9ilks Code World (๑•́ ₃ •̀๑) 个人主页: 9ilk (๑•́ ₃ •̀๑) 文章专栏&#xff1a; 算法Journey 本篇博客我们分享一下双指针算法中的快慢指针以及对撞双指针&#xff0c;下面我们开始今天的学习吧~ &#x1f3e0; 快乐数 &#x1f4d2; 题…