MockMvc 模拟请求

接口写好了,可以用单元测试。使用注解,返回统一数据格式 这里面是用postman来操作的, 如果还是要单元测试呢?   这时候可以用到MockMvc

代码:

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class TestRequestMvc {

    private MockMvc mockMvc;

    @Autowired
    WebApplicationContext wac;

    @Before
    public void setup() {
        // 初始化
        this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void testGetUnit() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/aop/getAopInfo")).andDo(print())
                .andExpect(MockMvcResultMatchers.status().isOk());
    }

    @Test
    public void testPostUnit() throws Exception {
        JSONObject json = new JSONObject();
        json.put("mvcMock", "try");
        mockMvc.perform(MockMvcRequestBuilders.post("/aop/postAopInfo").content(json.toJSONString())
                .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)).andDo(print())
                .andExpect(MockMvcResultMatchers.status().isOk());
    }
}
 

Post请求,要设置参数和参数的格式

请求的结果:

get请求:

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /aop/getAopInfo
       Parameters = {}
          Headers = []
             Body = <no character encoding set>
    Session Attrs = {}

Handler:
             Type = com.yan.studyAlone.spring.aop.controller.AopController
           Method = public java.lang.Object com.yan.studyAlone.spring.aop.controller.AopController.configureTasks()

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 = {"status":0,"message":null,"data":{"status":0,"message":null,"data":"I will do it","total":0},"total":0}
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

post请求:

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /aop/postAopInfo
       Parameters = {}
          Headers = [Content-Type:"application/json;charset=UTF-8"]
             Body = {"mvcMock":"try"}
    Session Attrs = {}

Handler:
             Type = com.yan.studyAlone.spring.aop.controller.AopController
           Method = public java.lang.Object com.yan.studyAlone.spring.aop.controller.AopController.getAops(com.alibaba.fastjson.JSONObject)

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 = {"status":0,"message":null,"data":{"status":0,"message":null,"data":{"mvcMock":"try","return":"do you best!"},"total":2},"total":0}
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

body 是目标数据

总结:

       使用MockMvc可以模拟postman的请求,在测试controller里面的url是比较方便的。如果经常要调试的,不确定性比较大的(有时候思路不是很清晰或是对内容不很清楚的),还是使用postman进行比较方便,可以多次请求,确定后再使用单元测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天狼1222

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

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

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

打赏作者

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

抵扣说明:

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

余额充值