Spring Boot 2.x使用Mockito进行测试
在上一篇,项目基本实现了Spring Boot对Mybatis的整合。这篇文章使用Mockito对项目进行测试。
1、使用postmat测试;
2、编写单元测试类,使用mockito进行测试;
3、使用idea内置工具进行测试
运行AicodeBgmsApplication.java,启动项目后,可以采用如下方式对接口进行测试。
一、使用postman进行测试
如上图所示进行测试,其他接口请自行测试。
二、编写单元测试类进行测试
这里使用Idea辅助我们创建单元测试类
在要测试的类,如:UserInfoController.java类中点击右键,再点击Go To,再点击Test,如下图所示:
或者点击菜单上的Navigate,然后点击Test,选择Create New Test...
然后进入下面界面,如下:
确定要测试的类和包路径,把编写单元测试的方法都选中,然后点击OK。单元测试类即可生成。
编写的测试代码如下:UserInfoControllerTest.java
-
package com.aicode.bgms.controller;
-
-
import org.junit.After;
-
import org.junit.Before;
-
import org.junit.Test;
-
import org.junit.runner.RunWith;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.boot.test.context.SpringBootTest;
-
import org.springframework.test.context.junit4.SpringRunner;
-
import org.springframework.test.web.servlet.MockMvc;
-
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
-
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
-
import org.springframework.transaction.annotation.Transactional;
-
import org.springframework.util.LinkedMultiValueMap;
-
import org.springframework.util.MultiValueMap;
-
import org.springframework.web.context.WebApplicationContext;
-
-
import static org.junit.Assert.*;
-
-
@RunWith(SpringRunner.class)
-
@Transactional
-
@SpringBootTest
-
public class UserInfoControllerTest {
-
-
private MockMvc mockMvc;
-
-
@Autowired
-
private WebApplicationContext wac;
-
-
@Before
-
public void setUp() throws Exception {
-
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
-
}
-
-
@After
-
public void tearDown() throws Exception {
-
}
-
-
@Test
-
public void list() throws Exception {
-
String mvcResult= mockMvc.perform(MockMvcRequestBuilders.get("/list"))
-
.andReturn().getResponse().getContentAsString();
-
System.out.println("Result === "+mvcResult);
-
}
-
-
@Test
-
public void add() throws Exception {
-
final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
-
params.add("userName", "test2");
-
params.add("password", "pass1234");
-
params.add("age", "12");
-
params.add("email", "test@aicode.com");
-
String mvcResult= mockMvc.perform(MockMvcRequestBuilders.post("/add")
-
.params(params)).andReturn().getResponse().getContentAsString();
-
System.out.println("Result === "+mvcResult);
-
}
-
-
@Test
-
public void get() throws Exception {
-
String mvcResult= mockMvc.perform(MockMvcRequestBuilders.get("/get/1"))
-
.andReturn().getResponse().getContentAsString();
-
System.out.println("Result === "+mvcResult);
-
}
-
-
@Test
-
public void modify() throws Exception {
-
final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
-
params.add("id", "1");
-
params.add("userName", "test1");
-
params.add("password", "123qwe");
-
params.add("age", "24");
-
params.add("email", "test@aicode.com");
-
String mvcResult= mockMvc.perform(MockMvcRequestBuilders.put("/edit")
-
.params(params)).andReturn().getResponse().getContentAsString();
-
System.out.println("Result === "+mvcResult);
-
}
-
-
@Test
-
public void del() throws Exception {
-
mockMvc.perform(MockMvcRequestBuilders.delete("/del/2"))
-
.andReturn();
-
String mvcResult= mockMvc.perform(MockMvcRequestBuilders.get("/list"))
-
.andReturn().getResponse().getContentAsString();
-
System.out.println("Result === "+mvcResult);
-
}
-
-
}
- @SpringBootTest —— SpringBoot 自 1.4.0 版本开始引入的一个用于测试的注解;
- @RunWith(SpringRunner.class) ——代表运行一个 Spring 容器;
- @Transactional——可以使单元测试进行事务回滚,以保证数据库表中没有因测试造成的垃圾数据,再就是保证单元测试可以反复执行;
- @Before—— 代表在测试启动时候需要提前加载的内容,这里是提前加载 MVC 环境。
执行UserInfoControllerTest.java,然后下面是执行单元测试的结果
关于MockMvc进行单元测试如果不太清楚,可以先百度一下做一下基本了解。
可以点击每个测试方法,查看具体的测试结果
这样,我们使用MockMvc就完成了对Controller层的测试。Service层和Dao层也可以使用MockMvc进行测试,这里就不再进行说明,请读者自行尝试。
三、使用Idea中的工具进行测试
运行AicodeBgmsApplication.java,启动项目。
点击菜单中的Tools——〉Test Restful Web Service,然后在窗口输入参数进行测试。
添加:
点击左侧的绿色右向三角标运行,然后可以看到返回结果。
查询:
点击左侧的绿色右向三角标运行,然后可以看到返回结果。
测试时,请求HTTP method的匹配。
原文地址:https://blog.csdn.net/zhenbie/article/details/84072282
Spring Boot 2.x使用Mockito进行测试的更多相关文章
- Spring Boot项目中使用Mockito
本文首发于个人网站:Spring Boot项目中使用Mockito Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试 ...
- Spring Boot -05- 多模块结构项目构建与测试(详细图文教程)IDEA 版
Spring Boot -05- 多模块结构项目构建与测试(详细图文教程)IDEA 版 百度很多博客都不详细,弄了半天才把 Spring Boot 多模块项目构建开发整的差不多,特地重新创建配置,记录 ...
- spring boot使用java读取配置文件,DateSource测试,BomCP测试,AnnotationConfigApplicationContext的DataSource注入
一.配置注解读取配置文件 (1)@PropertySource可以指定读取的配置文件,通过@Value注解获取值 实例: @PropertySource(val ...
- Spring Boot教程(十)异步方法测试
测试 测试代码如下: @Component public class AppRunner implements CommandLineRunner { private static final Log ...
- Spring Boot应用的测试——Mockito
Spring Boot应用的测试——Mockito Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试:Spring ...
- Spring Boot 1.4测试的改进
对Pivotal团队来说,工作上的好事情是他们拥有一个被叫做Pivotal Labs的灵活发展部门,拥有Labs团队的Lean 和 XP程序设计方法学的强大支持,例如结对编程和测试驱动开发.他们对于测 ...
- spring boot test中mockito的运用
mock的意义 在微服务盛行的当下,开发过程中往往出现A应用中某功能的实现需要调用B应用的接口,无论使用RPC还是restful都需要B应用提供接口的实现整个开发工作才能继续进行.从而导致A应用的开发 ...
- 在Spring Boot项目中使用Spock测试框架
本文首发于个人网站:在Spring Boot项目中使用Spock测试框架 Spock框架是基于Groovy语言的测试框架,Groovy与Java具备良好的互操作性,因此可以在Spring Boot项目 ...
- Spring Boot 解决方案 - JUnit 测试
简单的 JUnit 项目 回顾一下创建并运行简单的 JUnit 测试项目,先添加 JUnit 依赖然后编写类似如下模板的测试类,使用 IDE 的话直接用插件运行就行, 使用 Maven 的话运行命令 ...
随机推荐
- R语言实现分层抽样(Stratified Sampling)以iris数据集为例
R语言实现分层抽样(Stratified Sampling)以iris数据集为例 1.观察数据集 head(iris) Sampling)以iris数据集为例"> 选取数据集中前6个 ...
- Directx11教程(15) D3D11管线(4)
原文:Directx11教程(15) D3D11管线(4) 本章我们首先了解一下D3D11中的逻辑管线,认识一下管线中每个stage的含义. 参考资料:http://fgiesen.wordpress ...
- 从程序员的角度分析微信小程序
昨天朋友圈被微信小程序刷爆了. 我赶快在书架上拿出三年前买的书,把上面的土擦干净,压压惊. 作为一个并不是资深的程序员. 从程序员的角度分析一下微信小程序,欢迎指点. 首先吐槽 微信小程序只发了200 ...
- C++:for范围循环特点--自我理解
for(declaration : expression)statement for(xx-type i : P)....其一:for范围类型循环在循环前,可能会对p所在的队列里,对每一个对象进行一次 ...
- SQL if语句简要
if语句 可以作为表达式用 可以在存储过程中作为流程控制语句用 表达式 IF(条件,条件true,条件false) 示例 sex字段m,f 互换 update salary set sex = if( ...
- 高可用Kubernetes集群原理介绍
■ 文/ 天云软件 云平台开发工程师 张伟 1. 背景 Kubernetes作为容器应用的管理中心,对集群内部所有容器的生命周期进行管理,结合自身的健康检查及错误恢复机制,实现了集群内部应用层的高可用 ...
- python 集合创建
- oracle函数 MIN([distinct|all]x)
[功能]统计数据表选中行x列的最大值. [参数]all表示对所有的值求最大值,distinct只对不同的值求最大值,默认为all 如果有参数distinct或all,需有空格与x(列)隔开. [参数] ...
- Python 官方文档:入门教程
https://pythoncaff.com/docs/tutorial/3.7.0 官方入门教程,从这里开始你的 Python 之旅,将长久维护 基础信息 翻译说明 关于本教程 已完成 正文 1. ...
- 如何用Chrome浏览器下载网页音乐视频
打开网页,先让要下载的视频播放,右键单击选择审查元素(F12),选择上方的Network选项,按F5刷新,这个时候我们可以看到框架中Size下的不少文件数据数字正在变大,按size降序排列.点击表格的 ...