Class MockMultipartFile

java.lang.Object
org.springframework.mock.web.MockMultipartFile
All Implemented Interfaces:
org.springframework.core.io.InputStreamSource, org.springframework.web.multipart.MultipartFile

public class MockMultipartFile extends Object implements org.springframework.web.multipart.MultipartFile
Mock implementation of the MultipartFile interface.

Useful in conjunction with a MockMultipartHttpServletRequest for testing application controllers that access multipart uploads.

Since:
2.0
Author:
Juergen Hoeller, Eric Crampton
See Also:
  • Constructor Details

    • MockMultipartFile

      public MockMultipartFile(String name, @Nullable byte[] content)
      Create a new MockMultipartFile with the given content.
      Parameters:
      name - the name of the file
      content - the content of the file
    • MockMultipartFile

      public MockMultipartFile(String name, InputStream contentStream) throws IOException
      Create a new MockMultipartFile with the given content.
      Parameters:
      name - the name of the file
      contentStream - the content of the file as stream
      Throws:
      IOException - if reading from the stream failed
    • MockMultipartFile

      public MockMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content)
      Create a new MockMultipartFile with the given content.
      Parameters:
      name - the name of the file
      originalFilename - the original filename (as on the client's machine)
      contentType - the content type (if known)
      content - the content of the file
    • MockMultipartFile

      public MockMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, InputStream contentStream) throws IOException
      Create a new MockMultipartFile with the given content.
      Parameters:
      name - the name of the file
      originalFilename - the original filename (as on the client's machine)
      contentType - the content type (if known)
      contentStream - the content of the file as stream
      Throws:
      IOException - if reading from the stream failed
  • Method Details

    • getName

      public String getName()
      Specified by:
      getName in interface org.springframework.web.multipart.MultipartFile
    • getOriginalFilename

      @NonNull public String getOriginalFilename()
      Specified by:
      getOriginalFilename in interface org.springframework.web.multipart.MultipartFile
    • getContentType

      @Nullable public String getContentType()
      Specified by:
      getContentType in interface org.springframework.web.multipart.MultipartFile
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface org.springframework.web.multipart.MultipartFile
    • getSize

      public long getSize()
      Specified by:
      getSize in interface org.springframework.web.multipart.MultipartFile
    • getBytes

      public byte[] getBytes() throws IOException
      Specified by:
      getBytes in interface org.springframework.web.multipart.MultipartFile
      Throws:
      IOException
    • getInputStream

      public InputStream getInputStream() throws IOException
      Specified by:
      getInputStream in interface org.springframework.core.io.InputStreamSource
      Specified by:
      getInputStream in interface org.springframework.web.multipart.MultipartFile
      Throws:
      IOException
    • transferTo

      public void transferTo(File dest) throws IOException, IllegalStateException
      Specified by:
      transferTo in interface org.springframework.web.multipart.MultipartFile
      Throws:
      IOException
      IllegalStateException