为什么我要获得REST API删除呼叫的400个状态代码



我正在尝试使用Mockito模拟REST API调用。很多时候,我尝试执行以下测试案例,它在以下状态代码中失败:400

我已经检查了URI,URI通过也很好,但我想知道我缺少的地方。

@RequestMapping(value = "/todo/{id}", method = RequestMethod.DELETE)
public ResponseEntity<Response> removeToDoById(@PathVariable("id") long id, @RequestParam((value = "reason") String reason) ) throws ToDoException{
    logger.info("ToDo id to remove " + id);
    ToDo toDo = toDoService.getToDoById(id);
    if (toDo == null || toDo.getId() <= 0){
        throw new ToDoException("ToDo to delete doesn´t exist");
    }
    toDoService.removeToDo(toDo);
    return new ResponseEntity<Response>(new Response(HttpStatus.OK.value(), "ToDo has been deleted"), HttpStatus.OK);
}

以下是测试调用

@Test
public void verifyDeleteToDo() throws Exception {
    mockMvc.perform(MockMvcRequestBuilders.delete("/todo/4").accept(MediaType.APPLICATION_JSON))
    .andExpect(jsonPath("$.status").value(200))
    .andExpect(jsonPath("$.message").value("ToDo has been deleted"))
    .andDo(print());
}

您的方法需要其他参数reason,您在测试方法中不提供对 MOCKMVC 的要求。

@RequestMapping(value = "/todo/{id}", method = RequestMethod.DELETE)
public ResponseEntity<Response> removeToDoById(@PathVariable("id") long id, @RequestParam((value = "reason") String reason) ) throws ToDoException{

将其送给MockMVC

mockMvc.perform(delete("/todo/4")
                .param("reason", "bla-bla")
                // omitted

或将其标记为"不需要"

@RequestParam(required = false, value = "reason") String reason

相关内容

  • 没有找到相关文章

最新更新