SpringBoot MockMvc的单元测试
对于类的测试,可以有很多的方式进行实现,比如可以使用PostMan,使用HttpClient请求等,这里主要讲的是MockMcv的测试
一:引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
二:Demo如下:
package cn.wangtao.controller; import cn.wangtao.HTTPEntity.RequestEntity.TestRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Assert;
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.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import java.util.HashMap;
import java.util.Map; @RunWith(SpringRunner.class)
@SpringBootTest(classes = cn.wangtao.run.Application.class)//注意这里的类要引入的是Main入口类
public class TestControllerTest {
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
private ObjectMapper objectMapper=new ObjectMapper(); @Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); //构造MockMvc
} @Test
public void testModelParam() throws Exception {
TestRequest request = new TestRequest();
request.setAge(18);
request.setName("桃子");
request.setAddress("上海");
request.setServerType("ctr1");
Map<String, String> map = new HashMap<String, String>();
map.put("request",objectMapper.writeValueAsString(request));
modelParam("/test",objectMapper.writeValueAsString(request));
} @Test
public void testMoreParam() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("name","wangtao");
map.put("age","18");
map.put("address","上海");
map.put("serverType","ctr2");
moreParam("/test2",map);
} /**
* @Author wangtao
* @Description 对象模型接收
* @Date 2019-3-28 18:01
* @Param param:对象的json 结构
* @return
**/
public void modelParam(String requestPath, String param ) throws Exception {
MockHttpServletRequestBuilder mockHttpServletRequestBuilder
= MockMvcRequestBuilders.get(requestPath)//自己选择方式
.content(param)
.contentType(MediaType.APPLICATION_JSON_UTF8);
MvcResult result=mockMvc.perform(mockHttpServletRequestBuilder)
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
int statusCode = result.getResponse().getStatus();
String content = result.getResponse().getContentAsString();
System.out.println("statusCode: "+statusCode);
System.out.println("返回结果 content: "+content);
Assert.assertEquals(statusCode, 200);
} /**
* @Author wangtao
* @Description 一个或多个k-参数
* @Date 2019-3-28 17:59
* @Param requestPath:URI, map:参数集合
* @return
**/
public void moreParam(String requestPath, Map<String,String> map ) throws Exception {
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(requestPath);//请求方式自己定
//参数封装
for (Map.Entry<String,String> entry:map.entrySet() ){
mockHttpServletRequestBuilder.param(entry.getKey(),entry.getValue());
}
mockHttpServletRequestBuilder.contentType(MediaType.APPLICATION_JSON_UTF8);
MvcResult result=mockMvc.perform(mockHttpServletRequestBuilder)
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
int statusCode = result.getResponse().getStatus();
String content = result.getResponse().getContentAsString();
System.out.println("statusCode: "+statusCode);
System.out.println("返回结果 content: "+content);
Assert.assertEquals(statusCode, 200);
} }
SpringBoot MockMvc的单元测试的更多相关文章
- 【快学springboot】在springboot中写单元测试[Happyjava]
前言 很多公司都有写单元测试的硬性要求,在提交代码的时候,如果单测通不过或者说单元测试各种覆盖率不达标,会被拒绝合并代码.写单元测试,也是保证代码质量的一种方式. junit单元测试 相信绝大多数的J ...
- 【快学springboot】在springboot中写单元测试
前言 很多公司都有写单元测试的硬性要求,在提交代码的时候,如果单测通不过或者说单元测试各种覆盖率不达标,会被拒绝合并代码.写单元测试,也是保证代码质量的一种方式. junit单元测试 相信绝大多数的J ...
- springboot Service层单元测试
两个实现类实现同一个Service接口 public interface CustomUrlService { List<ShopMetrics> getShopMetrics(); } ...
- springboot+mybatis整合(单元测试,异常处理,日志管理,AOP)
我用的事IDEA,jdk版本是1.7.新建项目的时候这个地方的选择需要注意一下,springboot版本是1.5的,否则不支持1.7的jdk pom.xml <dependency> &l ...
- SpringBoot使用Junit4单元测试
SpringBoot2.0笔记 本篇介绍Springboot单元测试的一些基本操作,有人说一个合格的程序员必须熟练使用单元测试,接下来我们一起在Springboot项目中整合Junit4单元测试. 本 ...
- Java程序员的日常—— POI与JDBC、Mockmvc与单元测试
周日没怎么休息好,周一一天都迷迷糊糊的,不过还算是干了不少的活. 总结一下,大致有以下几点内容: 1 使用poi以及mysql jdbc实现了一个复杂excel的导入 2 基于工程原有的代码,书写sp ...
- springboot~mockMvc和asciidoctor生成基于TDD的API文档
API文档是前端与后端快速开发,减少沟通成本的必要条件,有一份完善的文档是很必要的,由通过测试来生成文档的好处就是:测试数据有了,测试返回结果有了,而且可以对这些字段进行说明,很清晰,在springb ...
- spring-boot 速成(11) - 单元测试
一.添加依赖项: testCompile 'org.springframework.boot:spring-boot-starter-test:1.5.2.RELEASE' 二.单元测试代码示例 im ...
- 【SpringBoot】SpringBoot配置与单元测试(二)
SpringBoot项目创建参考[SpringBoot]SpringBoot快速入门(一) 本文介绍SpringBoot项目的POM文件.配置与单元测试 POM文件 1.SpringBoot的pom文 ...
随机推荐
- ClamAV学习【2】——clamscan入口函数浏览
就简单给代码加上些注释,方便理解.第一次浏览,应该会有不正确的理解.后面会继续学习修改. 文件:clamscan\clamscan.c 代码如下: nt main(int argc, char **a ...
- java学习笔记—HTTP协议(10)
客户端浏览器和服务器Tomcat要进行基本的请求和响应的处理. 其实浏览器和服务器通信的数据的格式已经使用协议进行固定好了,只要双方发送固定格式的数据,那么就可以完成基本的通信. 如果开发者需要查看这 ...
- set 集合数据类型
set 数据类型 set 与列表类似,区别在于 set 不能包含重复的值. In [1]: a_list = ['a', 'b', 'c', 'b', 'd', 'm', 'n', 'n'] In [ ...
- Error: Cannot find module 'gulp-sass'
刚才首次启动ionic的时候,给我报了个这:Error: Cannot find module 'gulp-sass' 应该是缺少gulp-sass模块了,可又不敢贸然装,直接百度: stackove ...
- 异步 JavaScript - 事件循环
简评:如果你对 JavaScript 异步的原理感兴趣,这里有一篇不错的介绍. JavaScript 同步代码是如果工作的 在介绍 JavaScript 异步执行之前先来了解一下, JavaScrip ...
- 数学规划求解器lp_solve超详细教程
前言 最近小编学了运筹学中的单纯形法.于是,很快便按奈不住跳动的心.这不得不让我拿起纸和笔思考着,一个至关重要的问题:如何用单纯形法装一个完备的13? 恰巧,在我坐在图书馆陷入沉思的时候,一位漂亮的小 ...
- tomcat运行springboot项目war包
以最简单的spring boot demo项目来演示如何发布项目war包到tomcat,并成功运行(有很多小伙伴会出现404错误) 一.准备一个最简单的demo项目 在IDEA中新建一个项目,一直ne ...
- leetcode-479-Largest Palindrome Product(找到两个乘数相乘得到的最大的回文数)
题目描述: Find the largest palindrome made from the product of two n-digit numbers. Since the result cou ...
- video.js 应用于网站需要视频的
http://www.cnblogs.com/lechenging/p/3858181.html
- win10 安装 mysql 5.7
win10 安装 mysql 5.7 1.在mysql 官网下载mysql 5.7 的手动安装包 mysql--winx64.zip 2. 解压到mysql 目录的文件夹下面 D:\moudle\my ...