BindingResult不起作用 - Spring Test

时间:2013-06-12 19:03:50

标签: spring testing spring-mvc

我有我的控制器:

@RequestMapping(value = "/addtimesheet", method = RequestMethod.POST)
public String addTimeSheet(@ModelAttribute("timesheet")TimeSheet timeSheet,
                         BindingResult bindingResult,
                         ModelMap model) {
    if (bindingResult.hasErrors()) {
        if (bindingResult.hasFieldErrors("horaInicio")){
            model.addAttribute("messageHoraInicioError",HORA_INICIO_INVALIDA);
        }
        if (bindingResult.hasFieldErrors("horaFim")){
            model.addAttribute("messageHoraFimError",HORA_FIM_INVALIDA);
        }
        return TIMESHEETCRUD_NOVO;
    }


    return TIMESHEETCRUD_LIST;
}

当我尝试:

/时间表/ addtimesheet

字段“horainicio = null”,我的bindingResult.hasError()为true。

嗯,这是工作: - )

我的测试:

@Test
@ExpectedDatabase("timeSheetControllerIt.xml")
public void novoTimeSheetSemHoraInicial() throws Exception {
    TimeSheet novoTimeSheet = new TimeSheet();
    mockMvc.perform(
            post("/timesheet/addtimesheet")
            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
            .body(super.convertObjectToFormUrlEncodedBytes(novoTimeSheet))
            .sessionAttr("timesheet", novoTimeSheet)
    )
            .andDo(print())
            .andExpect(status().isOk())
            .andExpect(view().name("timesheetcrud/novo"))
            .andExpect(forwardedUrl("/WEB-INF/views/timesheetcrud/novo.jsp"))
            .andExpect(model().attribute("messageHoraInicioError", is("timesheetcontroller.horainicio.invalida")));
}

我在控制器中看到对象“timeSheet”为“horaInicio”为null,但我的bindingResult.hasErrors()为false。

任何人都知道出了什么问题?

感谢

0 个答案:

没有答案