title: Setting RequestBody properly of MockHttpServletRequestBuilder
body: <p>I am simply trying to test a Spring controller method using a <code>MockHttpServletRequestBuilder</code>. The signature of the controller method looks like this:</p>
  
  <pre><code> @RequestMapping(value = "/assignTeamsToUsers", method = RequestMethod.POST)
   public @ResponseBody String assignUsersToTeams(Model model, @RequestBody MultiValueMap&lt;String, String&gt; ids).
  </code></pre>
  
  <p>In my test case I have:</p>
  
  <pre><code>MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
  MockHttpServletRequestBuilder request = createRequest(uri, method);
  
  //set up request object...Not sure how??
  //My current attempt:
       String body = "userIds[]=0&amp;teamIds[]=0";
       request.content(body);
       request.accept(MediaType.ALL);
       request.contentType(MediaType.APPLICATION_FORM_URLENCODED);
  
  ResultActions resultActions = mockMvc.perform(request);
  </code></pre>
  
  <p>EDIT: Showing createRequest. <code>method</code> = "POST"</p>
  
  <pre><code>private MockHttpServletRequestBuilder createRequest (String uri, String method) {
      MockHttpServletRequestBuilder builder = null;
  
      if("GET".equalsIgnoreCase(method))
          builder = MockMvcRequestBuilders.get(uri);
      else if("POST".equalsIgnoreCase(method))
          builder = MockMvcRequestBuilders.post(uri);
      else
          Assert.fail("Unsupported method!");
  
      //We always create requests for user Manager
      builder.header("securityRole", Role.Manager.getDisplayName());
  
      return builder;
  
  }
  </code></pre>
  
  <p>I know the uri and method are correct. My problem is I am getting a <strong>415</strong> error code from Spring. Basically, I do not know how to set up the <code>request</code> object to have the appropriate <code>@RequestBody</code> for the <code>MultiValueMap</code>. I have tried alot of variations of setting request.content, setting the request.accept, request.contentType, request.characterEncoding, and still every time I get a 415 error. </p>
  
  <p>If it is any help, I can successfully Post to this endpoint using the web interface, and here is what the request looks like in chrome:</p>
  
  <p><img src="https://i.stack.imgur.com/c372L.png" alt="enter image description here">  </p>