IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> MockMvc GET请求处理 -> 正文阅读

[开发测试]MockMvc GET请求处理

?

学习使用MockMvc请求,post请求,参数都是一样的。Get请求,参数就不同了。对于不同类型的参数,改如何处理呢?

1,参数在URL上的Path

PathVariable

@ResponseBody
@GetMapping("/inst/{tableId}")
public Object getAopsInst(@PathVariable Long tableId) {
??? return tableId+" I will make it";
}

测试:

@Test
public void testPathParam() throws Exception {
??? Long tableId = 2022318L;
??? mockMvc.perform(MockMvcRequestBuilders.get("/aop/inst/"+tableId)).andDo(print())
??????????? .andExpect(MockMvcResultMatchers.status().isOk());
}

结果:

MockHttpServletResponse:
?????????? Status = 200
??? Error message = null
????????? Headers = [Content-Type:"text/plain;charset=UTF-8", Content-Length:"22"]
???? Content type = text/plain;charset=UTF-8
?????? ??????Body = 2022318 I will make it
??? Forwarded URL = null
?? Redirected URL = null
????????? Cookies = []

2,单个参数

单个RequestParam

@ResponseBody
@GetMapping("/getTypeInfoByType")
public Object getTypeInfoByType(@RequestParam("type") String type) {
??? return type+" single param";
}

测试:

@Test
public void testGetSingleParam() throws Exception {
??? String type = "ALARM_CLASS";
??? mockMvc.perform(MockMvcRequestBuilders.get("/aop/getTypeInfoByType").param("type", type)).andDo(print())
??????????? .andExpect(MockMvcResultMatchers.status().isOk());
}

结果:

MockHttpServletResponse:
?????????? Status = 200
??? Error message = null
????????? Headers = [Content-Type:"text/plain;charset=UTF-8", Content-Length:"24"]
???? Content type = text/plain;charset=UTF-8
???????????? Body = ALARM_CLASS single param
??? Forwarded URL = null
?? Redirected URL = null
????????? Cookies = []

3,多个参数:

多个RequestParam

@ResponseBody
@GetMapping("/getTypeInfoByTypes")
public Object getTypeInfoByTypes(@RequestParam("type") String type,
                                @RequestParam(value = "state", required = false) String state) {
    return "type: "+ type+" and state "+state +" double param";
}

处理:

多个时候,怎么处理呢?

public MockHttpServletRequestBuilder params(MultiValueMap<String, String> params)

MultiValueMap 具体实现 ?熟悉的是LinkedMultiValueMap

?MultiValueMap 是多指映射

?

测试:

@Test
public void testGetMulParams() throws Exception {
    String type = "ALARM_CLASS";
    String state = "0SA";
    MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
    params.put("type", Collections.singletonList(type));
    params.put("state", Collections.singletonList(state));
    mockMvc.perform(MockMvcRequestBuilders.get("/aop/getTypeInfoByTypes").params(params)).andDo(print())
            .andExpect(MockMvcResultMatchers.status().isOk());
}

结果:

MockHttpServletResponse:
?????????? Status = 200
??? Error message = null
????????? Headers = [Content-Type:"text/plain;charset=UTF-8", Content-Length:"44"]
???? Content type = text/plain;charset=UTF-8
???????????? Body = type: ALARM_CLASS and state 0SA double param
??? Forwarded URL = null
?? Redirected URL = null
????????? Cookies = []

总结:

????????使用MockMvc可以模拟postman的请求,post方式参数处理是一致的。Get方式内容会多一些。主要是多个的处理是使用MultiValueMap

?

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2022-05-02 13:31:17  更:2022-05-02 13:31:21 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 12:43:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码