我有这个 html 弹簧表单:
<form:form action="addVacancy" modelAttribute="myVacancy">
<form:label path="name">name</form:label>
<form:input path="name" ></form:input>
<form:errors path="name" cssClass="error" />
<br>
<form:label path="description">description</form:label>
<form:input path="description" id="nameInput"></form:input>
<form:errors path="description" cssClass="error" />
<br>
<form:label path="date">date</form:label>
<input type="date" name="date" />
<form:errors path="date" cssClass="error" />
<br>
<input type="submit" value="add" />
</form:form>
我用这种方法处理这个表格:
@RequestMapping("/addVacancy")
public ModelAndView addVacancy(@ModelAttribute("myVacancy") @Valid Vacancy vacancy,BindingResult result, Model model,RedirectAttributes redirectAttributes){
if(result.hasErrors()){
model.addAttribute("message","validation error");
return new ModelAndView("vacancyDetailsAdd");
}
vacancyService.add(vacancy);
ModelAndView mv = new ModelAndView("redirect:goToVacancyDetails");
mv.addObject("idVacancy", vacancy.getId());
redirectAttributes.addAttribute("message", "added correctly at "+ new Date());
return mv;
}
使用 MockMvc 如何传递与此 html 表单相同的数据?
@Test
public void testMethod(){
MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get("/addVacancy");
//what must I write here?
ResultActions result = mockMvc.perform(request);
}