Spring MVC 测试缺少来自 MockMvc 示例的导入

Spring MVC Test missing imports from MockMvc example

我正在尝试使用 Spring MVC, but even when using example from MockMvc:

进行测试
.andExpect(status().isOk())
.andExpect(content().mimeType("text/html"))

使用MockHttpServletRequestBuilder:

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

我遇到了 2 个异常:

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

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

我正在使用示例中的导入(如下),但我似乎仍然缺少其他导入

 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.*;

由于没有 post 编辑完整的代码,因此很难猜测会出现什么错误。我 post 在这里工作代码,这可能会有所帮助。

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