2

私はこのコントローラーメソッドを書きました:

  @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 を使用しています。

4

1 に答える 1