title: Cannot Test MockMultipartFile with Spring Boot Test - No serializer found
body: <p>I'm trying to mock the upload of an image to a controller endpoint which is expecting a DTO containing a MultipartFile input along with several plain text fields. But I can't seem to mock a MultipartFile to send:</p>
  
  <p>Here is my <strong>test:</strong></p>
  
  <pre><code> @Test
    public void saveAnEntryWhenPOSTNewUserWithAPicture() throws Exception {
      MockMultipartFile multiPFImage = new MockMultipartFile("contactImgUpload", "abcpic.png",
              "text/plain", "Generate bytes to simulate a picture".getBytes());
      mockMvc.perform(MockMvcRequestBuilders.fileUpload("/newContact")
              .file(multiPFImage)
              .contentType(MediaType.MULTIPART_FORM_DATA)
              .param("userId", "12345")
              .param("name", "Picture Uploader User"))
              .andExpect(status().isOk())
              .andExpect(content().string(containsString("Picture Uploader User")))
              .andExpect(content().string(containsString("Replace with image title")));
    }
  </code></pre>
  
  <p>The <strong>controller</strong> method we are testing:</p>
  
  <pre><code>@PostMapping(path = "/newContact")
    public @ResponseBody ContactDTO createNewContact(@ModelAttribute ContactDTO newContact) {
  
     //converts newContact to DAO and persists to DB
  
     return newContact
  }
  </code></pre>
  
  <p>The <strong>DTO</strong> for conversion:</p>
  
  <pre><code>public class ContactDTO implements Serializable {
  
    private BigInteger userId;
    private BigInteger contactId; //automatically generated on persistence
    private String name;
    private MultipartFile contactImgUpload;
  }
  </code></pre>
  
  <p>When I run the test, it fails, and I get this message:</p>
  
  <blockquote>
    <p>.w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP
    message:
    org.springframework.http.converter.HttpMessageNotWritableException:
    Could not write JSON: No serializer found for class
    java.io.ByteArrayInputStream and no properties discovered to create
    BeanSerializer (to avoid exception, disable
    SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is
    com.fasterxml.jackson.databind.JsonMappingException: No serializer
    found for class java.io.ByteArrayInputStream and no properties
    discovered to create BeanSerializer (to avoid exception, disable
    SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain:
    app.models.dto.ContactDTO["contactImgUpload"]->org.springframework.mock.web.MockMultipartFile["inputStream"])</p>
  </blockquote>
  
  <p>I've seen several other instances of this question, but most don't have answers, or aren't quite the same. Any ideas on how to test a  MockMultipartFile that needs to bind to the DTO?</p>