While trying to get my PUT MockMvc test to work, I found that JSON was not supported. Using an answer from this thread: Configure MappingJacksonHttpMessageConverter
I was able to resolve this by extending the WebMvcConfigurationSupport class. However, when I use this override, it appears as though the ModelAndView data returned in my GET tests is now NULL. I know I can get the response data using this:
String content = result.getResponse().getContentAsString();
But, does anyone have an explanation for why the ModelAndView data is NULL?
Here is my MockMVC test class:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("test-rest-context.xml")
public class AccountControllerTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
private MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(),
Charset.forName("utf8"));
@Before
public void setUp() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
}
@Test
public void findCustomerByID() throws Exception {
MvcResult result = mockMvc.perform(get("/api/account/customers/{customerID}", "123").accept(contentType)
.param("fields", "id", "email", "userName")
.contentType(contentType))
.andExpect(status().isOk())
.andReturn();
ModelAndView mav = result.getModelAndView();
// mav is NULL here after I extend WebMvcConfigurationSupport class.
}
}