7

How can I possibly write test case for boolean in mockito, spring mvc environment

For e.g, like the following response

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 = []

We would write the test case like,

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

Right? But, when we got the response like 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 = []

How should I write the test case?

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

1 Answers1

9

All you need to do is the following:

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

The meat of the above code the is the string method of ContentResultMatchers (returned by content()).

Here is the relevant javadoc

geoand
  • 49,266
  • 16
  • 140
  • 156