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公司中真正推行单测的很少很少,一些大厂大部分也只是在核心产品推广单测来保障质量,今天这 ...
随机推荐
- Educational Codeforces Round 55 Div. 2 翻车记
A:签到. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...
- 题解 P5015 【标题统计】
既然这个题这么水 大家不如来盘点一下算法呗 首先说一个事:逗号表达式 这玩意的值是最后一个表达式的值 那么我们就可以愉快的放进循环条件里摩擦 话说这个应该是基础吧,大多数代码都可以这样干 具体可以后面 ...
- ans menu list
ans menu list 1. 系统配置 a) 基本设置 i. NTP ii. 配置模式 iii. 主机信息 b) 高可用性 i. 节点 ii. 路由监视器 iii. 故障转移接口群 c) 设备标识 ...
- 【NOI2006】聪明的导游
[NOI2006]聪明的导游 题面 洛谷 题目描述 小佳最近迷上了导游这个工作,一天到晚想着带游客参观各处的景点.正好 M 市在举行 NOI,来参观的人特别的多.不少朋友给小佳介绍了需要导游的人. M ...
- 【ZOJ3316】Game(带花树)
[ZOJ3316]Game(带花树) 题面 Vjudge 翻译: 给定棋盘上\(n\)个旗子 一开始先手可以随便拿, 然后每次都不能取离上次的曼哈顿距离超过\(L\)的旗子 谁不能动谁输. 问后手能否 ...
- HTML5 表单自学记录
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- svn常见错误
1.svn提交报错:svn: Aborting commit:XXXXXremains in conflict 解决:说明Svn服务器上的对应内容,在你上次Update后已被别人修改了,而你也做了修改 ...
- codeblocks快捷键(转)
==日常编辑== • 按住Ctrl滚滚轮,代码的字体会随你心意变大变小.• 在编辑区按住右键可拖动代码,省去拉(尤其是横向)滚动条之麻烦:相关设置:Mouse Drag Scrolling.• Ctr ...
- centOS7 vsftp ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS) 启动失败问题?
[root@localhost c]# systemctl status vsftpd.service ● vsftpd.service - Vsftpd ftp daemon Loaded: loa ...
- C语言实现栈(顺序存储方式)
#include <stdio.h> #include <stdlib.h> //提供malloc()原型 #include <stdbool.h> //提供tru ...