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 的话运行命令 ...
随机推荐
- python2与python3爬虫中get与post对比
python2中的urllib2改为python3中的urllib.request 四种方式对比: python2的get: # coding=utf-8 import urllib import u ...
- Directx11教程(14) D3D11管线(3)
原文:Directx11教程(14) D3D11管线(3) 现在我们开始学习一些CP(command processor)的知识.参考资料: http://fgiesen.wordpres ...
- oracle审计的格式
audit table; audit table by xxx(username); audit table by xxx(username) whenever not successful; 系统表 ...
- js错误处理Try-catch和throw
1.try-catch语句 Try{ //可能会导致错误的代码 }catch(error){ //在错误发生时怎么处理 } 例如: try{ window.someNonexistentFunct ...
- Nginx 缓存代理
访问ArcGIS官网的地图瓦片太慢.想到可用NIGIX代理. Nginx是Linux下http代理软件,Windows下也有. 以下为配置方法,注意红色部分. 1.需要在本地proxy_cache_p ...
- javascript中数字的一些常规操作
1,禁止输入 - (减号.负号) // html <input type="number" class="no-negative"> // js $ ...
- spring的父子关系
1.父容器不能拿子容器的资源 2.子容器可以拿到父容器的资源
- LeetCode Weekly Contest 6
leetcode现在每周末举办比赛,这是今天上午参加的比赛的题解.题目难度不算大,两个easy,一个medium,一个hard.hard题之前接触过,所以做得比较顺利. 1. Sum of Left ...
- 详解Python中内置的NotImplemented类型的用法
它是什么? ? 1 2 >>> type(NotImplemented) <type 'NotImplementedType'> NotImplemented 是Pyth ...
- Linux 运算符
布尔运算符 下表列出了常用的布尔运算符,假定变量a为10 变量b为20: 运算符 说明 举例 ! 非运算 , 表达式为true 则返回false 否则返回true [!false] 返回true. ...