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公司中真正推行单测的很少很少,一些大厂大部分也只是在核心产品推广单测来保障质量,今天这 ...
随机推荐
- C++解析(27):数组、智能指针与单例类模板
0.目录 1.数组类模板 1.1 类模板高效率求和 1.2 数组类模板 1.3 堆数组类模板 2.智能指针类模板 2.1 使用智能指针 2.2 智能指针类模板 3.单例类模板 3.1 实现单例模式 3 ...
- CF623D birthday 贪心 概率期望
题意:n个人,玩抓人游戏,每抓住一个人都要猜这个人是谁.对于每一局,第i个人有$p_{i}$的概率被抓到.游戏结束当且仅当每个人都在某局中被抓到并且猜中自己的名字,求一个合适的策略来使得期望游戏局数最 ...
- 制作VR视频播放器
最近VR火的不要不要的,但是综合起来,VR资源最多的还是全景图片和全景视频,今天在这里给大家简单介绍一下如何用Unity制作简单的VR视频播放器. 首先找到EasyMovieTexture这个插件,A ...
- bzoj3938 Robot
3938: Robot Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 336 Solved: 112[Submit][Status][Discuss ...
- Java Socket UDP编程
package com; import java.io.IOException; import java.net.*; /** * UDP Client * * Created by Administ ...
- portal商品展示功能逻辑
看下接口: 返回值: 门户商品搜索功能的实现: 根据分类id进行搜索,根据关键词进行搜索,并按照一定的顺序排序 业务逻辑: 1.查询分类是否存在. 2.如果分类存在,则递归分类,展示父类商品,子类商品 ...
- Linux常用网络工具:路由扫描之mtr
除了上一篇<Linux常用网络工具:路由扫描之traceroute>介绍的traceroute之外,一般Linux还内置了另一个常用的路由扫描工具mtr. mtr在某些方面比tracero ...
- 用for语句从数组中剔除数据,注意,count,要放到for语句之外才行
date_default_timezone_set('Asia/Shanghai'); $arr = array( ,), ,), ,), ,) ); print_r($arr); ;$i<co ...
- --BEA官方网站(http: //www.bea.com)甲骨文已完成对该公司的收购BEA Weblogic Server 7.0x应用服务器简明安 装、配置手册 1
====================简 介: BEA公司是业内著名的中间件产商,以Tuxedo及Weblogic闻名于世,而其基础件平台(infrastructure)Weblogic platf ...
- struts2的action中@Autowired注入为null的解决方案
今天遇到类似问题,记录下来以便以后查阅: @Aspect作用于action,致使action中的@Autowired注入为null的解决方案,以下三种任选一种: 1.去掉@Autowired,改用se ...