title: post or put form data in mock mvc
body: <p>I want to write test for a controller that only accepts form data:</p>
  <pre class="lang-java prettyprint-override"><code>   @PostMapping(&quot;/signup/&quot;)
      public ResponseEntity&lt;?&gt; signUpUser(@ModelAttribute UserModel user, HttpServletResponse response)
              throws Exception {
          return userService.signUpUser(user, response);
      }
  </code></pre>
  <p>I used to call this method in postman like this:
  <a href="https://i.stack.imgur.com/jdTlk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jdTlk.png" alt="enter image description here" /></a></p>
  <p>The problem is i don't know how to <strong>exactly</strong> simulate this request.</p>
  <p>I wrote a test but fails saying that data are not passed:</p>
  <pre class="lang-java prettyprint-override"><code>    @Test
      @Order(1)
      @WithMockUser(username = &quot;anonymousUser&quot;)
      void signUpUser() throws Exception {
          var user = new UserModel();
          user.setEmail(&quot;email@mail.com&quot;);
          MockMultipartFile file1 = new MockMultipartFile(&quot;profileFile&quot;, &quot;hello.jpg&quot;, MediaType.IMAGE_JPEG_VALUE,
                  &quot;Hello, World!&quot;.getBytes());
          MockMultipartFile file2 = new MockMultipartFile(&quot;shopFile&quot;, &quot;hello.jpg&quot;, MediaType.IMAGE_JPEG_VALUE,
                  &quot;Hello, World!&quot;.getBytes());
          user.setProfileFile(file1);
          user.setShopFile(file2);
          user.setAddress(&quot;address&quot;);
          user.setDescription(&quot;desc&quot;);
          user.setUserName(&quot;user n&quot;);
          user.setPassword(&quot;pass1&quot;);
          user.setPasswordRepeat(&quot;pass1&quot;);
          mockMvc.perform(post(&quot;/api/user/signup/&quot;)
                  .contentType(MediaType.MULTIPART_FORM_DATA)
                  .accept(MediaType.APPLICATION_JSON)
                  .content(mapToJson(user))
                  )
                  .andExpect(status().isOk())
                  .andExpect(jsonPath(&quot;$&quot;).isMap())
                  .andDo(print())
                  .andDo(result -&gt; {
                      JSONObject obj = new JSONObject(result.getResponse().getContentAsString());
                      userId = obj.getLong(&quot;id&quot;);
                  });
      }
  </code></pre>