MockMvc control layer unit test parameters delivery problems

GET:

1. The path parameter @PathVariable

2. Form parameters @RequestParam

 

POST:

1.JSON request body parameters

  @RequestBody

 

put:

1. The path parameter @PathVariable

2. Form parameters @RequestParam

 

 

delete:

1. The path parameter @PathVariable

2. Form parameters @RequestParam

 

MockMvc:

1. The path request

mockMvc.perform (MockMvcRequestBuilders
        . Request mode ( "url / {path}" , the parameter value)
2. Request Form

mockMvc.perform (MockMvcRequestBuilders
        . Request mode ( "url"). param ( " key", "value") .contentType (MediaType.APPLICATION_FORM_URLENCODED)
3.JSON request

MvcResult mvcResult= mvc.perform(
        MockMvcRequestBuilders.post("http://127.0.0.1:8080/index").
                content(jsonObject.toString()).
                contentType(MediaType.APPLICATION_JSON)
).
        andExpect(MockMvcResultMatchers.status().isOk()).
        andDo(MockMvcResultHandlers.print()).
        andReturn();

Original Address: https: //blog.csdn.net/wang_muhuo/article/details/84655577

Guess you like

Origin www.cnblogs.com/jpfss/p/10966481.html