How to test binders / property editors used on spring 2.5 controllers

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
}

How to check the binding result? If I call a method with a user and the result of the binding, I will not test the binding process. I suppose there is something that accepts MockHttpServletRequest and returns the model and binding result, any suggestions?

+2
source share
2 answers

Are you trying to test the binding (what happens before calling this method) or are you trying to test the send method of the handler?

You can check 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...

    }
+4

, , spring-test-mvc , , . , , , , API, .

+3

Source: https://habr.com/ru/post/1751622/


All Articles