Mockmvc put 方法不起作用 spring

Mockmvc put method is not working spring

我正在为我的 spring restful 服务编写测试用例。我们在controller中有一个put服务,语法如下

@RequestMapping(value="/update", method=RequestMethod.PUT)
public @ResponseBody List<PaidUpResponse> updateStatus(
       @RequestBody @Valid PaidUpRequest paidUpRequest,
       HttpServletRequest request, HttpServletResponse response) {
}

写测试用例,我用了下面的方法

mockMvc.perform(put("/update").contentType(UnitTestUtil.APPLICATION_JSON_UTF8)
            .content(UnitTestUtil.convertObjectToJsonBytes(request)))
            .andExpect(status().isOk());

但是它给出了编译错误,说 "The method put(String) is undefined"。你能建议我如何测试 put 方法吗?

您必须导入适当的依赖项:

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;