I have my controller:
@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;
}
when I try:
/timesheet/addtimesheet
with the field "horainicio = null", my bindingResult.hasError() is true.
Well, It's work :-)
My test:
@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")));
}
I saw in my controller that object "timeSheet" is "horaInicio" null, but my bindingResult.hasErrors() is false.
Anybody have idea what's wrong ?
thanks