0

我有这个控制器方法:

@RequestMapping(value = "/addEvent", method = RequestMethod.POST)
    public String addEvent(Model model,
            @Valid @ModelAttribute("myEvent") Event event,
            BindingResult result, RedirectAttributes redirectAttributes,
            @RequestParam(required = true) Integer selectedEventTypeId,
            @RequestParam(required = true) Integer selectedEventStatusId) {

        if (result.getErrorCount() > 1 ){
            return "eventDetailsAdd";
        }
        eventService.addEvent(event, selectedEventTypeId, selectedEventStatusId);
        redirectAttributes.addAttribute("idEvent", event.getId());
        redirectAttributes.addAttribute("message", "added correctly at " + new Date() );
        return "redirect:eventDetails";
    }

使用 mockMvc 我想模拟结果并检查两个变量if (result.getErrorCount() > 1 )

我怎么能做到?

4

1 回答 1

0

MockMvc你不能。MockMvc是一种 HTTP 客户端。您使用 生成 HTTP 请求MockMvcRequestBuilders,设置您的标头、请求参数、正文和 URL。MockMvc将模拟发送集成完整DispatcherServlet堆栈的请求。

如果你想嘲笑这个BindingResult论点,我建议你@Controller独立测试你的课程。

// in @Test
MyController controller = new MyController(/* maybe other arguments */)
// generate your mocks and declare expectations
controller.addEvent(/* mocks go here */);
// verify your mocks
于 2013-10-16T15:04:29.577 回答