Search code examples
javaspringspring-mvcspring-mvc-test

Spring MVC Test missing imports from MockMvc example


I'm trying to test using Spring MVC, but even when using example from MockMvc:

.andExpect(status().isOk())
.andExpect(content().mimeType("text/html"))

Using MockHttpServletRequestBuilder:

MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders
        .post("/servlet/api/update")

I'm getting 2 exceptions:

The method andExpect(ResultMatcher) is undefined for the type MockHttpServletRequestBuilder

The method mimeType(String) is undefined for the type ContentResultMatchers

I'm using the import from example (below), but still seems that I'm missing other imports

 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
 import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;

Solution

  • Since the complete code is not posted it's difficult to guess what would be the mistake. I post working code here, that might help.

             mockMvc.perform(post(path)
                        .contentType(APPLICATION_JSON)
                        .accept(APPLICATION_JSON)
                        .header(AUTHORIZATION, BEARER_AUTHORIZATION)
                        .content(mapper.writeValueAsString(webRequest)))
                .andExpect(status().isCreated())