Outputting the result of a mockMvc call

How to output the result of a mockmvc call to help with debugging

Lee Turner

1 minute read

This post falls into the category of something I always forget and need to Google.

When running spring integration tests and using mockMvc, it can often be useful to see the output of the call in the console to help with debugging. This can be done by adding MockMvcResultsHandlers.print() to the call chain like in the following example.

This:

@Test  
void getStatementsThrowABadRequestWhenCookiesNotPresent() throws Exception {  
   mockMvc.perform(get("/statements")  
	 .headers(getHeaders())  
	 .cookie(getCookies())  
	 .contentType(MediaType.APPLICATION_JSON));
	 .andExpect(status().isBadRequest())  
	 .andExpect(jsonPath("$.status", is(400)))  
	 .andExpect(jsonPath("$.type", is("BAD_REQUEST")))  
	 .andExpect(jsonPath("$.errors.length()", is(0)));
}

Becomes this:

@Test  
void getStatementsThrowABadRequestWhenCookiesNotPresent() throws Exception {  
   mockMvc.perform(get("/statements")  
	 .headers(getHeaders())  
	 .cookie(getCookies())  
	 .contentType(MediaType.APPLICATION_JSON));
	 .andExpect(status().isBadRequest())  
	 .andExpect(jsonPath("$.status", is(400)))  
	 .andExpect(jsonPath("$.type", is("BAD_REQUEST")))  
	 .andExpect(jsonPath("$.errors.length()", is(0)))
	 .andDo(MockMvcResultHandlers.print());
}

Along with all the other output from the test, there will be the json response from the call to the endpoint.

comments powered by Disqus