我写了这个控制器方法:
@RequestMapping("/submitFormAdd")
public ModelAndView submitFormAdd(
Model model,
@ModelAttribute("myCandidate") @Valid Candidate myCandidate,
BindingResult result,
RedirectAttributes redirectAttributes) {
if (result.hasErrors()) {
return new ModelAndView("candidateDetailsAdd");
}
myCandidate.setDate(new Date());
candidateService.add(myCandidate);
redirectAttributes.addAttribute("message", "added Correctly at "+ new Date() );
redirectAttributes.addAttribute("id", myCandidate.getId());
ModelAndView modelAndView = new ModelAndView(
"redirect:loadCandidateById");
return modelAndView;
}
更新
myCandidate 实例所以:
@ModelAttribute(value = "myCandidate")
public Candidate loadCandidateById(
@RequestParam(required = false) Integer candidateId) {
if (candidateId != null)
return candidateService.findById(candidateId);
return null;
}
我可以用什么方法测试这个方法是否得到 Candidate ?
我使用 MockMvc。