如何测试弹簧2.5控制器上使用的粘合剂/属性编辑器 [英] How to test binders/property editors used on spring 2.5 controllers

查看:99
本文介绍了如何测试弹簧2.5控制器上使用的粘合剂/属性编辑器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注释控制器,其方法可以预期模型和绑定结果

I have an annotated controller with a method that expects a model and a binding result

@RequestMapping(method = RequestMethod.POST)
  public ModelAndView submit(@ModelAttribute("user") User user, BindingResult bindingResult) {
     //do something
}

如何测试绑定结果?如果我使用用户和绑定结果调用该方法,那么我没有测试绑定过程。我想这里有一个需要一个MockHttpServletRequest并返回模型和绑定结果的任何建议?

How do I test the binding result? If I call the method with a user and a binding result then I'm not testing the binding process. I figure there myst be something that takes a MockHttpServletRequest and returns the model and the binding result, any suggestions?

推荐答案

你在尝试测试绑定(发生在此方法被调用之前发生),或者您是否尝试测试提交处理程序方法?

Are you trying to test the binding (which happens before this method would be called) or are you trying to test the "submit" handler method?

您可以使用类似于这个:

You can test the binding with something like this:

 @Test
    public void testHandlerMethod() {

        final MockHttpServletRequest request = new MockHttpServletRequest("post", "/...");
        request.setParameter("firstName", "Joe");
        request.setParameter("lastName", "Smith");

        final User user = new User();
        final WebDataBinder binder = new WebDataBinder(user, "user");
        binder.bind(new MutablePropertyValues(request.getParameterMap()));

        final ModelAndView mv = controllerTestInstance.submit(user, binder.getBindingResult());

        // Asserts...

    }

这篇关于如何测试弹簧2.5控制器上使用的粘合剂/属性编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆