springBoot单元测试-模拟MVC测试
1)模拟mvc测试,和基础测试是一样的, 都需要在pom文件中引入junit的支持。
略
2)编写测试类 Application1TestMVC
在类头上除啦加入之前的@RunWith(SpringRunner.class)、@RunWith(SpringRunner.class) 之外还要加入新的注解
@AutoConfigureMockMvc // 注入MockMvc
(当然你实在不想加也行,有其他办法 , 不过我不想说,麻烦)
package com.cx.springboot; import java.util.Date; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
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.MockMvcRequestBuilders; import com.alibaba.fastjson.JSON;
import com.cx.springboot.hello1.model.UserModel; @RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc // 注入MockMvc
public class Application1TestMVC { @Autowired
private MockMvc mvc; /**
*
* @throws Exception
* @创建时间 2018年7月13日
* @功能描述 通过链接传值 , 接受string 返回值
*/
@Test
public void testString() throws Exception {
//准备请求url 不用带ip、端口、项目名称等 直接写接口的映射地址就可以了
String url = "/app/get2/zhangsan/1"; /* 构建request 发送请求GET请求
* MockMvcRequestBuilders 中有很多 请求方式。像get、post、put、delete等等
*/
MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(url)
.accept(MediaType.APPLICATION_JSON)) // 断言返回结果是json
.andReturn();// 得到返回结果 MockHttpServletResponse response = mvcResult.getResponse();
//拿到请求返回码
int status = response.getStatus();
//拿到结果
String contentAsString = response.getContentAsString(); System.err.println(status);
System.err.println(contentAsString);
} /**
*
* @throws Exception
* @创建时间 2018年7月13日
* @功能描述 传递header ,接受 返回值
*/
@Test
public void headerTest() throws Exception {
// uri
String uri = "/app/get4"; MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri)
.header("token", "asd123")
.header("name", "zhangsan11")
.accept(MediaType.APPLICATION_JSON)) // 断言返回结果是json
.andReturn();// 得到返回结果 MockHttpServletResponse response = mvcResult.getResponse();
//拿到请求返回码
int status = response.getStatus();
//拿到结果
String contentAsString = response.getContentAsString(); System.err.println(status);
System.err.println(contentAsString);
}
/**
*
* @throws Exception
* @创建时间 2018年7月13日
* @功能描述 传递post请求和 bean类型对象 ,接受 返回值
*/
@Test
public void postTest() throws Exception {
// uri
String uri = "/app/get3"; UserModel userModel = new UserModel("张三", 11, new Date(), "abc123"); MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(uri)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(JSON.toJSONString(userModel))
.accept(MediaType.APPLICATION_JSON)) // 断言返回结果是json
.andReturn();// 得到返回结果 MockHttpServletResponse response = mvcResult.getResponse();
//拿到请求返回码
int status = response.getStatus();
//拿到结果
String contentAsString = response.getContentAsString(); System.err.println(status);
System.err.println(contentAsString);
}
}
springBoot单元测试-模拟MVC测试的更多相关文章
- springboot-32-使用mvc测试
Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试:Spring Boot可以跟BDD(Behavier Driven ...
- Spring MVC测试框架
原文链接:http://jinnianshilongnian.iteye.com/blog/2004660 Spring MVC测试框架详解——服务端测试 博客分类: springmvc杂谈 spri ...
- Spring MVC测试框架详解——服务端测试
随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的.从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用sp ...
- SpringBoot单元测试的两种形式
@ 目录 前言 demo环境 springbootTest Junit 总结 前言 最近公司要求2021年所有的项目代码单元测试覆盖率要达到90%,作为刚毕业的小白来说这简直就是噩梦啊,springb ...
- Webpack单元测试,e2e测试
此篇文章是续 webpack多入口文件.热更新等体验,主要说明单元测试与e2e测试的基本配置以及相关应用. 一.单元测试 实现单元测试框架的搭建.es6语法的应用.以及测试覆盖率的引入. 1. 需要安 ...
- SpringBoot单元测试中的事务和Session
1.Springboot中使用junit编写单元测试,并且测试结果不影响数据库. 2.
- spring-boot单元测试
一.为什么要写单元测试 很多程序员有两件事情不愿意做: 写注释. 写单元测试. 但是在看代码时又会希望有清晰明了的注释,重构代码时能有一套随时可以跑起来的单元测试. 最近在迁移一个项目,从sqlser ...
- .netcore持续集成测试篇之MVC测试
前面我们讲的很多单元测试的的方法和技巧不论是在.net core和.net framework里面都是通用的,但是mvc项目里有一种比较特殊的类是Controller,首先Controller类的返回 ...
- Springboot单元测试Junit深度实践
Springboot单元测试Junit深度实践 前言 单元测试的好处估计大家也都知道了,但是大家可以发现在国内IT公司中真正推行单测的很少很少,一些大厂大部分也只是在核心产品推广单测来保障质量,今天这 ...
随机推荐
- Find the hotel HDU - 3193(RMQ)
题意: 有n个旅馆,从这n个旅馆中找出若干个旅馆,使得这若干个旅馆满足这样的条件:不能从其它和剩下的旅馆中找到一个价格和距离都小于这个旅馆的旅馆... 解析: 按price 排序,若price相同, ...
- Zebras CodeForces - 950C(思维)
借鉴自: https://www.cnblogs.com/SuuT/p/8619227.html https://blog.csdn.net/my_sunshine26/article/details ...
- TCP的拥塞控制 (一)
拥塞控制不同于流量控制,拥塞控制是在拥塞发生时,发送方根据一定的反馈,主动调节自己的发送速率,以防止拥塞恶化的行为. 1. 网络拥塞 路由器是网络中的关键组件,其内部有一定量的缓冲区,用于缓存来不 ...
- 删除docker网络docker0
yum -y install bridge-utils ifconfig docker0 down brctl delbr docker0
- dalao&话
最大权闭合子图 正负点权之间连边,容量为无穷大,代表正负之间有联系,跑最小割,要么舍弃正的要么舍弃负的,就是把图割开
- 58到家mysql数据库军规及解读分享
一.基础规范 (1)必须使用InnoDB存储引擎 解读:支持事务.行级锁.并发性能更好.CPU及内存缓存页优化使得资源利用率更高 (2)必须使用UTF8字符集 解读:万国码,无需转码,无乱码风险,节省 ...
- docker操作mysql
Docker操作mysql 查找docker hub上的mysql镜像 Docker search.mysql 拉取官方的镜像标签为5.6 Docker pull mysql:5.6 在本地镜像列表里 ...
- "Access restriction: The type BASE64Encoder is not accessible due to restrict"问题解决
问题如题: Eclipse中有一种叫做存取限制的机制,来防止你错误使用那些非共享的API.通常来说,Eclipse做的是对的,因为两点,我们不想要使用非共享API的,而且Eclipse知道什么是共享的 ...
- Linux上怎么快速删除一个目录
删除文件需要用到rm命令,但删除目录需要添加两个参数: -r 向下递归,不管多少级目录都删除 -f 强行删除,不做提示 #rm -rf 文件目录名
- Flying to the Mars
D - Flying to the Mars Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I ...