你怎么可能在mockito,spring mvc环境中为boolean编写测试用例

时间:2014-07-10 16:05:58

标签: spring-mvc mockito springmockito

我怎样才能在mockito,spring mvc environment

中为boolean编写测试用例

例如,如下面的回复

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");

右?但是,当我们得到响应时,请按照

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(???);

1 个答案:

答案 0 :(得分:6)

您需要做的就是:

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

以上代码的内容是string的{​​{1}}方法(由ContentResultMatchers返回)。

Here是相关的javadoc