title: Testing a spring controller method having @ModelAttribute as parameter
body: <p>I am trying to test a controller with this method:</p>
  
  <p>@RequestMapping(value="/test")<br>
  public ModelAndView generateRecords(@ModelAttribute("Employee") Employee employee) {</p>
  
  <p>And I would like to know how can I create a unit testing for testing this. At the moment I am using:</p>
  
  <pre><code>MockHttpServletRequest request = new MockHttpServletRequest();
  request.setRequestURI("/test");
  //request.setMethod("GET");
  new AnnotationMethodHandlerAdapter().handle(request, 
          new MockHttpServletResponse(), this.controller);
  </code></pre>
  
  <p>Running this test result in NULL value for ModelAttribute (Employee)</p>
  
  <p>Is there any way to pass modelattribute object to Controller when doing integration testing??</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p>Just to summarize: </p>
  
  <p><strong>Solution to this problem is pick the html element names and fill the paramter values in MockHttpRequest object and pass it over.</strong></p>
  
  <p>Example:</p>
  
  <pre><code>MockHttpServletRequest httpServletRequest = MockRequestResponseGenerator.mockRequest(getServletInstance().getServletContext(), "POST", "/test", paramters);
  
  //These paramters must be part of the ModelAttribute Object. Make sure, you are using custom property binding in case you have different object.
  
          httpServletRequest.setParameter("name", "SPRING MVC INTEGRATION TEST 
  TEMP");
          httpServletRequest.setParameter("id", "1");
          httpServletRequest.setParameter("desc", "SPRING MVC INTEGRATION TEST DESC");
  
  
          getServletInstance().service(httpServletRequest, httpServletResponse);
  </code></pre>