Is there a method built in spring MockMVC to get json content as Object?

As far as I know MockHttpServletResponse (Unlike RestTemplate) doesn’t have any method which could convert returned JSON to a particular type.

So what you could do is use Jackson ObjectMapper to convert JSON string to a particular type

Something like this

String json = rt.getResponse().getContentAsString();
SomeClass someClass = new ObjectMapper().readValue(json, SomeClass.class);

This will give you more control for you to assert different things.

Having said that, MockMvc::perform returns ResultActions which has a method andExpect which takes ResultMatcher. This has a lot of options to test the resulting json without converting it to an object.

For example

mvc.perform(  .....
                ......
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.firstname").value("john"))
                .andExpect(jsonPath("$.lastname").value("doe"))
                .andReturn();

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)