title: Integration Testing POSTing an entire object to Spring MVC controller
body: <p>Is there a way to pass an entire form object on mock request when integration testing a spring mvc web app? All I can find is to pass each field separately as a param like this:</p>
  
  <pre><code>mockMvc.perform(post("/somehwere/new").param("items[0].value","value"));
  </code></pre>
  
  <p>Which is fine for small forms. But what if my posted object gets larger? Also it makes the test code look nicer if I can just post an entire object.</p>
  
  <p>Specifically I'd like to test the selection of multiple items by checkbox and then posting them. Of course I could just test posting a single item, but I was wondering.. </p>
  
  <p>We're using spring 3.2.2 with the spring-test-mvc included.</p>
  
  <p>My Model for the form looks something like this:</p>
  
  <pre><code>NewObject {
      List&lt;Item&gt; selection;
  }
  </code></pre>
  
  <p>I've tried calls like this:</p>
  
  <pre><code>mockMvc.perform(post("/somehwere/new").requestAttr("newObject", newObject) 
  </code></pre>
  
  <p>to a Controller like this:</p>
  
  <pre><code>@Controller
  @RequestMapping(value = "/somewhere/new")
  public class SomewhereController {
  
      @RequestMapping(method = RequestMethod.POST)
      public String post(
              @ModelAttribute("newObject") NewObject newObject) {
          // ...
      }
  </code></pre>
  
  <p>But the object will be empty (yes I've filled it before in the test)</p>
  
  <p>The only working solution I found was using @SessionAttribute like this: 
  <a href="http://www.petrikainulainen.net/programming/spring-framework/integration-testing-of-spring-mvc-applications-forms/" rel="noreferrer">Integration Testing of Spring MVC Applications: Forms</a></p>
  
  <p>But I dislike the idea of having to remember to call complete at the end of every controller where I need this. After all the form data does not have to be inside the session, I only need it for the one request.</p>
  
  <p>So the only thing I can think of right now is to write some Util class that uses the MockHttpServletRequestBuilder to append all the object fields as .param using reflections or individually for each test case..</p>
  
  <p>I don't know, feeld un-intuitive.. </p>
  
  <p>Any thoughts / ideas on how I might make my like easier? (Apart from just calling the controller directly)</p>
  
  <p>Thanks!</p>