How can you possibly write test case for boolean in mockito, spring mvc environment
Asked Answered
T

1

11

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(???);
Tarrasa answered 10/7, 2014 at 16:5 Comment(0)
M
18

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

Margret answered 10/7, 2014 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.