我正在迁移Spring MVC控制器以使用更新的样式注释,并希望单元测试验证命令对象的控制器方法(请参阅下面的简单示例)。使用注释模仿Spring MVC BindingResult
@RequestMapping(method = RequestMethod.POST)
public String doThing(Command command, BindingResult result,
HttpServletRequest request, HttpServletResponse response,
Map<String, Object> model){
ThingValidator validator = new ThingValidator();
validator.validate(command, result);
... other logic here
}
我的问题是我要叫我的单元测试控制器的方法,并提供模拟值,以满足其签名恰当地执行代码,我不能工作,如何嘲笑一个BindingResult。
在老式控制器中,签名只需要一个HttpServletRequest和HttpServletResponse,它们很容易实现,但由于新注释风格的灵活性,必须通过签名传递更多内容。
如何模拟一个单元测试中使用的Spring BindingResult?
嗨马克, ,这让我在正确的轨道谢谢。使用一个 BindingResult bindingResult = new BeanPropertyBindingResult(command,“command”);并且在我的测试中在模型中粘贴命令对象似乎将我的测试排除在外。 – 2009-05-18 14:23:19
我也是这么做的。 – 2009-05-20 01:23:31