主要记录一下控制器的测试,service这些类测试相对简单些(可测试性强)

API测试需求比较简单:

  ① 需要返回正确的http状态码 200

  ② 需要返回json数据,并且不能返回未经捕获的系统异常

测试不通过例子

此测试类的部分代码

package cn.taxiong.search.web.controller;

import cn.taxiong.search.Application;
import cn.taxiong.search.constant.ErrorCodeMsgEnum;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SearchControllerTest { private MockMvc mockMvc; @Autowired
protected WebApplicationContext wac; @Before() //这个方法在每个方法执行之前都会执行一遍
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); //初始化MockMvc对象
} @Test
public void shopSearch() throws Exception { mockMvc.perform(
MockMvcRequestBuilders.get("/shopSearch").accept(MediaType.APPLICATION_JSON_UTF8)
.param("page", "1")
.param("pageSize", "2")
.param("startDate", "2018-01-01")
.param("endDate", "2028-01-01")
.param("keyword", "较场尾")
.param("lat", "22.22")
.param("lon", "42.22")
.param("distance", "5000")
.param("capacity", "2")
.param("style", "3")
.param("gt", "1")
.param("lt", "9900")
.param("keywordScope", "0")
.param("sort", "DEFAULT")
.param("service", "{6,7}")
.param("facilities", "{5,6}")
.param("shopType", "1")
.param("goodsCategory", "1")
)
.andExpect(status().isOk()) // 判断返回状态
.andExpect(content().contentType("application/json;charset=UTF-8")) // 判断内容类型
.andExpect(jsonPath("$.code", Matchers.not(ErrorCodeMsgEnum.SYSTEM_ERROR.getCode()))) // 如果是系统异常(未捕获的异常),则测试不通过
.andDo(print()); //打印出请求和相应的内容
}
}

测试通过例子:

当然,如果要返回码为正确的代码时才能测试通过可以这样写

 // 判断只有返回状态码为0才通过
.andExpect(jsonPath("$.code").value(ErrorCodeMsgEnum.SUCCESS.getCode()))
// 如果是系统异常(未捕获的异常),则测试不通过
//.andExpect(jsonPath("$.code", Matchers.not(ErrorCodeMsgEnum.SYSTEM_ERROR.getCode())))

最新文章

  1. Linux下安装py-leveldb
  2. java byte转无符号int
  3. sqlite数据库中 保存和读取UIData对象
  4. sc.WholeTextFiles与sc.textFile区别
  5. API、ABI区别
  6. linux nginx启动 重启 关闭命令
  7. Java中各种排序算法
  8. github配置和git学习
  9. PuTTY + Xming 远程使用 Linux GUI
  10. cf Strings of Power
  11. Aix 文件名补齐及aix6.1 bash安装
  12. BZOJ 3236: [Ahoi2013]作业( 莫队 + BIT )
  13. 【Alpha阶段】第一次Scrum Meeting!
  14. spark计算两个DataFrame的差集、交集、合集
  15. 16. Antimalware (反病毒 3个)
  16. Ubuntu 18.04基本配置
  17. Linux:Ubuntu系统的安装
  18. sql分页语句 速度比较快
  19. 2018.10.30 NOIP训练 【模板】树链剖分(换根树剖)
  20. IE上如何设置input type=file的光标不闪烁

热门文章

  1. python函数返回局部变量,局部&全局变量同名问题
  2. 2016"百度之星" - 初赛(Astar Round2A) A.All X 矩阵快速幂
  3. php 格式化时间
  4. 用 SQL 对关系型数据库进行查询
  5. 【Python】远离 Python 最差实践,避免挖坑
  6. Spring MVC配置静态资源和资源包
  7. hdu5618
  8. sql 时间段内没有的数据等于0
  9. openfalcon源码分析之hbs
  10. Python3 字典Dict(十三)