1

私は自分のコントローラーを持っています:

@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;
}

私が試したとき:

/timesheet/addtimesheet

フィールド「horanicio = 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 です。

誰が何が悪いのか考えていますか?

ありがとう

4

0 に答える 0