一、前言

使用mockito测试框架可以方便的进行web测试

二、用法实例

package com.ray.weixin.qy.controller;

import com.ray.weixin.qy.ApplicationTests;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.http.MediaType; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; /**
* @author : shira
* @date : 2018/7/8
* @time : 15:03
* @desc :
**/
@Slf4j
public class UserControllerTest extends ApplicationTests { /**
* 1.新增用户信息
* @throws Exception
*/
@Test
public void testCreate() throws Exception {
String content = "{\n" +
"\"userid\":\"sunwukong\",\n" +
"\"name\":\"孙悟空\",\n" +
"\"department\":[2],\n" +
"\"position\":\"总经理\",\n" +
"\"mobile\":\"17636763734\",\n" +
"\"gender\":\"0\",\n" +
"\"email\":\"17636763734@qq.com\"\n" +
"\n" +
"\n" +
"}"; String result = mockMvc.perform(
post("/user")
.content(content)
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andExpect(jsonPath("$.status").value(0))
.andReturn().getResponse().getContentAsString(); log.info(result);
} /**
* 2.删除用户信息
* @throws Exception
*/
@Test
public void testDelete() throws Exception { String userId="sunwukong"; String result = mockMvc.perform(
delete("/user")
.param("userId", userId)
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andExpect(jsonPath("$.status").value(0))
.andReturn().getResponse().getContentAsString(); log.info(result);
} /**
* 3.修改用户信息
* @throws Exception
*/
@Test
public void testUpdate() throws Exception {
String content = "{\n" +
"\"userid\":\"sunwukong\",\n" +
"\"name\":\"孙悟空\",\n" +
"\"department\":[2],\n" +
"\"position\":\"总经理\",\n" +
"\"mobile\":\"17636763734\",\n" +
"\"gender\":\"0\",\n" +
"\"email\":\"17636763734@qq.com\"\n" +
"\n" +
"\n" +
"}";
String result = mockMvc.perform(
put("/user")
.content(content)
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andExpect(jsonPath("$.status").value(0))
.andReturn().getResponse().getContentAsString(); log.info(result);
} /**
* 4.获取用户信息
* @throws Exception
*/
@Test
public void testGet() throws Exception { String userId="sunwukong"; String result = mockMvc.perform(
get("/user")
.param("userid", userId)
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
//.andExpect(jsonPath("$.length()").value(3))
.andExpect(jsonPath("$.status").value(0))
.andReturn().getResponse().getContentAsString(); log.info(result);
} }

三、用法详解

四、参考资料

1.SpringBoot与JUnit+Mockito 单元测试

最新文章

  1. 【原创分享·微信支付】 C# MVC 微信支付教程系列之公众号支付
  2. git怎么创建本地版本仓库
  3. PowerManager和WakeLock的操作步骤
  4. 无锁编程(一) - Double-checked Locking
  5. Android adb shell命令大全
  6. 初学QML之qmlRegisterType
  7. Swift - 标签条(UITabBar)标签页控制器(UITabBarController)用法
  8. jfinal使用配置文件注意事情
  9. 快速获取表单多条数据,使用ajax传递给后台
  10. 「深度剖析」程序员因为奇葩需求暴打pm,然后被双双开除
  11. zabbix系列之九——添加钉钉告警
  12. IIS版本号可以被识别 解决方案
  13. 给出两个单词word1和word2,写一个函数计算出将word1 转换为word2的最少操作次数。
  14. (转)Java动态追踪技术探究
  15. JS开发工具WebStorm使用快捷键
  16. js实现Mac触摸板双指事件(上/下/左/右/放大/缩小)
  17. bzoj 2806 [Ctsc2012]Cheat——广义后缀自动机+单调队列优化DP
  18. 第四篇——Spring音乐登录界面设计及实现(C#)
  19. linux 内核升级 转
  20. bzoj 3105

热门文章

  1. nginx基础系列
  2. MySQL数据库(2)_MySQL数据库和数据库表操作语句
  3. C# 调用VS自带程序WebDev.WebServer40.EXE 源代码
  4. SOA宣言和微服务特点
  5. perl FAQ(zz)
  6. 【P2774】方格取数问题(贪心+最大流,洛谷)
  7. sqlserver的疑难杂症解析
  8. R语言学习笔记(2)
  9. session共享个人小结
  10. Codeforces Round #425 (Div. 2) D 树链剖分 + 树状数组维护区间