【问题标题】:Spring MVC Test missing imports from MockMvc exampleSpring MVC 测试缺少来自 MockMvc 示例的导入
【发布时间】:2019-02-26 15:32:15
【问题描述】:

我正在尝试使用Spring MVC 进行测试,但即使使用来自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.*;

【问题讨论】:

    标签: java spring spring-mvc spring-mvc-test


    【解决方案1】:

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

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

    【讨论】:

    • status().isCreated() 为 MockHttpServletRequestBuilder 类型产生相同的错误The method andExpect(ResultMatcher) is undefined
    • 我在上一个未决问题stackoverflow.com/questions/54867824/…中的代码
    猜你喜欢
    • 2013-07-06
    • 2020-04-19
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 2014-05-31
    • 2019-07-30
    • 1970-01-01
    相关资源
    最近更新 更多