【问题标题】:SpringMVC testing post method with params带参数的 SpringMVC 测试 post 方法
【发布时间】:2017-01-05 21:48:51
【问题描述】:

我正在为 SpringMvc 应用程序编写测试,但遇到了一些问题。

这是我的控制器测试方法:

@RequestMapping(value = "submit", method = RequestMethod.POST, params = "delete")
public String delete(@ModelAttribute("inputBean") InputBean inputBean) {
    Book book = (Book) itemServiceController.getItemFindByIdService().findById(inputBean.getId());
    Author author = getAuthorOfBook(book);
    author.getBookList().remove(book);
    authorServiceController.getAuthorCreateService().create(authorServiceController.getAuthorUpdateService().update(author));
    itemServiceController.getItemDeleteService().delete(inputBean.getId());
    return "redirect:index.html";
}

这是我的 mockMvc:

mockMvc.perform(post("/submit?delete")
            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
            .param("id", "1")
            .sessionAttr("inputBean", new InputBean())
            )
    .andExpect(status().isOk())
    .andExpect(view().name("indexLib"))
    .andExpect(model().attribute("inputBean", hasProperty("id", is(1))));

这将返回状态 http 状态 400。

控制器方法用于 4 个表单提交按钮中的 1 个。如何解决这个问题?

【问题讨论】:

  • 你得到的实际异常是什么?

标签: java spring-mvc-test


【解决方案1】:

我找到了答案,正确的网址应该是

"/submit?id=102&delete="

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2020-11-19
    • 2022-11-12
    相关资源
    最近更新 更多