Rest接口单元测试

Get请求url不超过4000字节
Rest成熟度:level 0:使用http作为传输方式,leve 1:引入资源概念,每个资源有对应的url,level 2:使用http方法进行不同操作,使用http状态码表示不同结果,level 3:使用超媒体,在资源表达式中包含链接信息,一般做到level 2
测试依赖:org.springframework.boot:spring-boot-starter-test
测试类:
package com.example.security;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class) //使用SpringRunner运行
@SpringBootTest //表明这是一个测试用例
public class SecurityApplicationTests {
@Autowired
private WebApplicationContext wac; //注入web环境,但是并不会启动Tomcat,所以很快
private MockMvc mockMvc; //模拟web环境
@Before //@Before会在每个@Test之前执行,在里面初始化模拟的web环境
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
}
Rest请求单元测试举例(推荐先写好测试用例,执行,失败红色,然后去写Rest api,过程中不断执行测试用例,直到测试用例通过):
@Test
public void queryUsersOK() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/users")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(2));
}
该测试用例向/users发送get请求,contentType为application/json;charset=UTF-8,期望响应状态码200,期望响应json字符串数组长度为2。其中jsonPath是用来解析响应的json。请求格式错误会报400。请求的method不匹配会报405。
这里的$代表返回的json文档,具体可在github搜索jsonPath第一个项目json-path/JsonPath即可,具体使用参考https://github.com/json-path/JsonPath的Operators和Functions(方法)

注:测试用例中MockMvcRequestBuilders和MockMvcResultMatchers两个静态类常用,可以添加到STS的偏好设置,菜单栏Preferences--Java--Editor--Content Assist--Favorites--New Type(直接在Preferences搜索fa即可)
添加到偏好设置以后,自动变成静态引用,代码简化:
//静态引用
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
//测试用例
@Test
public void queryByParam() throws Exception {
String result = mockMvc.perform(get("/user")
// .param("username", "wangzhongshan")
// .param("password", "kibana")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
// .andExpect(jsonPath("$.length()").value(1));
.andExpect(jsonPath("$username").value("wzs")) //断言返回json对象的属性的值
.andReturn().getResponse().getContentAsString()//获取响应的字符串值
}
若要传递参数,如图注释,添加一到多个.param(String key, String value)即可,生成/user?key=value的查询,后台可用同名参数或对象的同名属性接受之,无论后台接受参数/参数对象属性是什么类型,测试用例的param之value都传字符串类型。
单元测试执行技巧:在@Test方法上执行run,先执行@Before方法再执行@Test方法,在空白位置执行run,所有@Test都会执行(每个@Test执行之前都会执行@Before),在package上执行run,则包下面所有的测试类的@Test都将执行
常用注解:
@RestController,@RequestMapping及变体(如@GetMapping,其源码可知是被@RequestMapping注解了并指定了相应的method)
@RequestParam(参数require默认true,必传,value是name属性的别名,两个都是给参数指定名字)
@PathVariable(映射url片段到参数,属性require默认true,必填,value是name属性的别名。url声明可以使用正则表达式,如@RequestMapping(value = "/user/{id:\\d+}")规范id只能传整数,否则报400
新学注解:@PageableDefault指定分页参数默认值(rest api传输类建包DTO)
org.springframework.data.domain.Pageable(Spring Data提供)
controller可使用该对象接受page,size,sort(排序,值如age,desc)三个分页参数值,也可以在用@PageableDefault注解Pageable对象,其提供page,size,sort这3个属性给Pageable提供默认值,代码:
@RequestMapping("/users")
public List<User> users(@PageableDefault(page = 0, size=10, sort = "username, asc") Pageable page) {
page.getPageNumber();
page.getPageSize();
page.getSort();
return new ArrayList<>();
}
post请求:参数为json对象,映射到controller方法的参数对象必须要用@RequestBody注解,否则json参数无法映射到参数对象,参数的属性只能拿到Null。
测试用例(post携带content传参):
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
@Test
public void createUser() throws Exception {
String content = "{\"username\":\"test\",\"pasword\":\"123456\",\"date\":" + System.currentTimeMillis()+ "}";
String result = mockMvc.perform(post("/user").contentType(MediaType.APPLICATION_JSON_UTF8).content(content)) //put与post的测试用例写法相同,只是把post改成put,url改成如/usr/1
.andExpect(status().isOk()).andExpect(jsonPath("$.id").value("1"))
.andReturn().getResponse().getContentAsString();
System.out.println(result); //时间属性,向后台传参是System.currentTImeMillis();,返回的是2019-08-24T04:06:49.154+0000
}
如果参数对象有Date属性,可以用yyyy-MM-dd或yyyy-MM-dd HH:mm:ss等时间格式字符串转换,但是需要转换且需要适配不同的展示格式,因此直接在json中传递时间戳(毫秒值)更方便,入库存时间戳,前台展示什么样式由js控制。前台时间戳和后台Date类型能够自由转换(经测试,从浏览器url传的date=时间戳转换失败)。
删除单元测试(请求方法使用多了,直接引用*即可):
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
@Test
public void deleteUser() throws Exception {
mockMvc.perform(delete("/user/1").contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(status().isOk());
}

posted @ 2019-09-11 23:39  发挥哥  阅读(2242)  评论(0编辑  收藏  举报