你怎么可能写测试用例布尔在mockito, spring mvc环境



我怎么可能在mockit中编写布尔测试用例,spring mvc环境

例如,像下面的响应

MockHttpServletResponse:
          Status = 200
   Error message = null
         Headers = {Content-Type=[application/json;charset=UTF-8]}
    Content type = application/json;charset=UTF-8
            Body = {"name":"myName","DOB":"12345"}
   Forwarded URL = null
  Redirected URL = null
         Cookies = []
我们可以这样写测试用例:
mockMvc.perform(get("/reqMapping/methodName"))
                    .andExpect(status().isOk())
                    .andExpect(content().contentType("application/json;charset=UTF-8")) 
                    .andExpect(jsonPath("$.name",comparesEqualTo("myName"); 
                    .andExpect(jsonPath("$.DOB",comparesEqualTo("12345");

对吧?但是,当我们得到像follow

这样的响应时
MockHttpServletResponse:
          Status = 200
   Error message = null
         Headers = {Content-Type=[application/json;charset=UTF-8]}
    Content type = application/json;charset=UTF-8
            **Body = true**
   Forwarded URL = null
  Redirected URL = null
         Cookies = []

我应该如何编写测试用例?

mockMvc.perform(get("/reqMapping/methodName"))
                    .andExpect(status().isOk())
                    .andExpect(content().contentType("application/json;charset=UTF-8")) 
                    .andExpect(???);

您所需要做的就是:

mockMvc.perform(get("/reqMapping/methodName"))
                    .andExpect(status().isOk())
                    .andExpect(content().contentType("application/json;charset=UTF-8")) 
                    .andExpect(content().string("true");

上述代码的核心是ContentResultMatchersstring方法(由content()返回)。

这里是相关的javadoc

相关内容

  • 没有找到相关文章

最新更新