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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天狼1222

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值