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公司中真正推行单测的很少很少,一些大厂大部分也只是在核心产品推广单测来保障质量,今天这 ...
随机推荐
- 怎样搭建一个自有域名的 WORDPRESS 博客?
博客搭建并不复杂,只是过程有点繁琐,适合喜欢折腾的人,主要有下面几个步骤: 新建一个博客文件 购买域名(Domain Name) 注册一个主机空间(Web Host) 域名解析(DNSPod) 安装W ...
- oracle 11g用户名的大小写问题
oracle 11g 用户名和密码默认区分大小写,可更改alter system set sec_case_sensitive_logon=false 设置改为不区分大小写.
- 【BZOJ3105】新Nim游戏(线性基)
[BZOJ3105]新Nim游戏(线性基) 题面 BZOJ Description 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以 ...
- Unity3D for VR 学习(1): 又一个新玩具 暴风魔镜 4(Android)
2016年伊始,有了VR虚拟现实硬件设备: 暴风魔镜4–好奇者的新玩具 . 2015年下半年的朋友圈中各种VR.AR的新闻层次不穷,搞的我也心痒痒的:好歹咱也是职业的Unity3D程序员,高大上的O ...
- 64位win10系统无法安装.Net framework3.5的两种解决方法【转】
近日有网友反映在windows10_64位系统电脑上安装Net framework3.5,操作时总失败,怎么办呢?小编下面就介绍win10 64位系统无法安装Net framework3.5的两种解决 ...
- bzoj2006: [NOI2010]超级钢琴(堆+RMQ)
和上一道题同类型...都是用堆求第k大 考虑对于每一个r,怎么求出一个最优的l.显然只需要求出前缀和,用RMQ查询前面最小的l的前缀和就好了.但是对于一个r,每个l只能选一次,选了一次之后,考虑怎么把 ...
- 解题:SDOI 2013 保护出题人
题面 首先是愉快的推式子 $dp[i]=max(dp[i],\frac{sum[i]-sum[j-1]}{x[i]+(i-j)*d})(1<=j<=i<=n)$(考虑有一只僵尸正好走 ...
- sleep php函数
<?php echo date('h:i:s') . "<br />"; //暂停 10 秒 sleep(10); //重新开始 echo date('h:i:s ...
- c#代码访问https服务器以及https的webservice
代码访问https类似浏览器操作 1.验证证书 2.如果要求客户端证书,提供客户端证书 具体代码如下: 访问https的web public static void ProcessRequest() ...
- 居中div,居中浮动的元素
定位法:position:absolute 如果子级div有定义宽和高的话就可以用这个方法.注意:margin-top,和margin-left的值均为高和宽值的一半 margin:auto法 这个也 ...