首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用ModelAttribute测试MockMvc参数

如何用ModelAttribute测试MockMvc参数
EN

Stack Overflow用户
提问于 2013-10-03 13:23:50
回答 1查看 1.7K关注 0票数 2

我编写了这个控制器方法:

代码语言:javascript
运行
复制
  @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实例化如下:

代码语言:javascript
运行
复制
@ModelAttribute(value = "myCandidate")
    public Candidate loadCandidateById(
            @RequestParam(required = false) Integer candidateId) {
        if (candidateId != null)
            return candidateService.findById(candidateId);
        return null;
    }

怎样测试这个方法才能获得候选信息?

我用MockMvc。

EN

回答 1

Stack Overflow用户

发布于 2013-10-03 13:53:29

您无法具体测试该方法是否将接收Candidate参数。您的选项是发送糟糕的请求数据,从而使Spring无法正确实例化它并返回一个400错误(但由于其他原因可能会发生这种情况)。或者发送正确的数据并收到你通常期望的结果。

请注意,您不应该测试Spring功能。当您使用第三方框架时,您假设它已经过正确的测试,特别是像Spring这样的测试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19160391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档