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);
}
}

最新文章

  1. [译]MVC网站教程(二):异常管理
  2. Spring远程调用技术<3>-Spring的HTTP Invoker
  3. php设计模式 工厂、单例、注册树模式
  4. Hibernate框架之get和load方法的区别
  5. adobe pro破解说明
  6. 解決BufferedReader读取UTF-8文件中文乱码
  7. Nodejs学习笔记(十)--- 与MongoDB的交互(mongodb/node-mongodb-native)、MongoDB入门
  8. ajax回调中的this.href不执行跳转的解决办法
  9. 百度地图秘钥ak的获取
  10. [转]Oracle SOME,ANY,All,EXISTS,IN
  11. paper 73 :HDR(High Dynamic Range Imaging)在摄影中指高动态范围成像
  12. sqlplus中常用设置参数
  13. WPC文件修改还原pin进度
  14. Content-Type: application/www-form-urlencoded
  15. vue 二三倍图适配,1像素边框
  16. Ext.net 的锁定列要集中放到前面
  17. struts2框架之标签
  18. LiquiBase 学习
  19. centos7.4 64位安装 google-chrome 与 chromedriver 运行 Python selenium 项目
  20. leetcode200

热门文章

  1. 【JavaScript】20款漂亮的css字体
  2. [三]SpringBoot 之 热部署
  3. BZOJ4921 互质序列
  4. C++解析(7):函数重载分析
  5. linux系统常见命令以及操作
  6. Python之旅:装饰器
  7. 「Python」socket指南
  8. TypeError: only integer scalar arrays can be converted to a scalar index
  9. NDK编译时两 .so之间调用问题
  10. VC6完整项目代码升级到VS2010