Elegant programming - MockMvc

MockMvc realized simulation Http request can be directly used in the form of a network, switch to invoke the Controller so that the test speed can not rely on the network environment, but also provides a means of verification, so that the request may validate the tax and very convenient, the most convenient for customers to test, start the server does not need to test our Rest Api.

SpringMVC example written Rest

Controller Example

@Controller
@RequestMapping(value = "/test")
public class TestWebController {

    @ResponseBody
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public List getMock(@RequestParam(name = "id", required = false, defaultValue = "l0") String id) {
        return Arrays.asList("l1", "l2", "l3", id);
    }

    @ResponseBody
    @RequestMapping(value = "/pust", method = {RequestMethod.POST})
    public Object postMock(@RequestBody Object data) {
        return ImmutableMap.<String, String>builder().put("data", JSON.toJSONString(data)).build();
    }
	
	 @RequestMapping(value = "/view", method = {RequestMethod.GET})
    public ModelAndView viewMock(){
        return new ModelAndView("index");
    }
}
复制代码

MockMvc example test Rest

  1. MockMvc foundation interaction example
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
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.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:context/spring-context.xml", "classpath:context/spring-mvc.xml"})
@WebAppConfiguration(value = "src/main/webapp")
public class TestController {

    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void test01() throws Exception {
        MvcResult mvcResult = this.mockMvc.perform(MockMvcRequestBuilders.get("/test/list")
                .cookie(new Cookie("token", "F3AF5F1D14F534F677XF3A00E352C"))   // 登录授权
                .accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
                .andExpect(handler().handlerType(TestWebController.class))  // 验证执行的控制器类型
                .andExpect(handler().methodName("getMock"))                 // 验证执行的方法名
                .andExpect(status().isOk())                                 // 验证执行状态
                .andDo(print())                                             // 打印交互信息
                .andReturn();
        System.out.println(mvcResult.getResponse().getContentAsString());
    }
}
复制代码

MockMvc can be simulated in one call controller; 1, mockMvc.perform execution request; 2, MockMvcRequestBuilders.get (urlTemplate, urlMultiParams) a request for configuration 3, ResultActions.andExpect assertion added after completion of execution of 4, ResultActions.andDo added a processor result, said to do something with the results, such as used here MockMvcResultHandlers.print () result information in response to the output of the whole. 5, ResultActions.andReturn represents return the results after the execution is completed.

Results of the:

["l1","l2","l3","l0"]
复制代码

Interaction information:

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /test/list
       Parameters = {}
          Headers = {Accept=[application/json;charset=UTF-8]}

Handler:
             Type = com.simple.example.web.TestWebController
           Method = public java.util.List com.simple.example.web.TestWebController.getMock(java.lang.String)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = {Content-Type=[application/json;charset=UTF-8]}
     Content type = application/json;charset=UTF-8
             Body = ["l1","l2","l3","l0"]
    Forwarded URL = null
   Redirected URL = null
          Cookies = []
复制代码
  1. Request Parameters Example MockMvc
@Test
    public void test02() throws Exception {
        MvcResult mvcResult = mockMvc.perform(get("/test/list?id={id}", "模板参数")
                .accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
                .andExpect(handler().handlerType(TestWebController.class))  // 验证执行的控制器类型
                .andExpect(handler().methodName("getMock"))                 // 验证执行的方法名
                .andExpect(status().isOk())                                 // 验证执行状态
                .andDo(print())                                             // 打印交互信息
                .andReturn();
        System.out.println(mvcResult.getResponse().getContentAsString());
    }
复制代码

Results of the:

["l1","l2","l3","模板参数"]
复制代码
  1. MockMvc example Test Post
@Test
public void test() throws Exception {
   MvcResult mvcResult = mockMvc.perform(post("/test/pust")
                .contentType(MediaType.APPLICATION_JSON)
                .content(JSON.toString(Arrays.asList("p1", "p2")))
                .accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
                .andExpect(handler().handlerType(TestWebController.class))  // 验证执行的控制器类型
                .andExpect(handler().methodName("getMock"))                 // 验证执行的方法名
                .andExpect(status().isOk())                                 // 验证执行状态
                .andDo(print())                                             // 打印交互信息
                .andReturn();
        System.out.println(mvcResult.getResponse().getContentAsString());
}
复制代码

Interactive information

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /test/pust
       Parameters = {}
          Headers = {Content-Type=[application/json], Accept=[application/json;charset=UTF-8]}

Handler:
             Type = com.simple.example.web.TestWebController
           Method = public java.lang.Object com.simple.example.web.TestWebController.postMock(java.lang.Object)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = {Content-Type=[application/json;charset=UTF-8]}
     Content type = application/json;charset=UTF-8
             Body = {"data":"[\"p1\",\"p2\"]"}
    Forwarded URL = null
   Redirected URL = null
          Cookies = []
复制代码

Results of the:

{"data":"[\"p1\",\"p2\"]"}
复制代码
  1. View test page
@Test
public void test05() throws Exception {
   MvcResult mvcResult = mockMvc.perform(get("/test/view"))
           .andExpect(view().name("index"))  //验证视图页面
           .andExpect(model().hasNoErrors()) //验证页面没有错误
           .andExpect(status().isOk())       //验证状态码
           .andDo(print())
           .andReturn();
   System.out.println(mvcResult.getModelAndView().getViewName());
}
复制代码

Results of the:

index
复制代码

MockMvc depth study

Guess you like

Origin juejin.im/post/5dfb2bdfe51d4557f42b74db