title: How to create CommonsMultipartFile object given only a file
body: <p>I have a junit test method that takes a CommonsMultipartFile object as a parameter.</p>
  
  <p>I'm trying to create a FileItem object so I can pass it to the constructor,</p>
  
  <pre><code>CommonsMultipartFile(org.apache.commons.fileupload.FileItem fileItem)
  </code></pre>
  
  <p>To do that, I'm trying to create the FileItem object using the DiskFileItem constructor,</p>
  
  <pre><code>DiskFileItem(java.lang.String fieldName, java.lang.String contentType, boolean isFormField, java.lang.String fileName, int sizeThreshold, java.io.File repository)
  </code></pre>
  
  <p>but I'm not sure how to pass any of those parameters.</p>
  
  <p>I have all of this working in a Spring 3 MVC controller, but to do my junit tests, I need to pass a method two objects. One is the UploadItem object which looks like the following,</p>
  
  <pre><code>import org.springframework.web.multipart.commons.CommonsMultipartFile;
  
  public class UploadItem {
   private String fileName;
   private String filePath;
   private CommonsMultipartFile fileData;
  
   public String getFileName() {
    return fileName;
   }
  
   public void setFileName(String fileName) {
    this.fileName = fileName;
   }
  
   public String getFilePath() {
    return filePath;
   }
  
   public void setFilePath(String filePath) {
    this.filePath = filePath;
   }
  
   public CommonsMultipartFile getFileData() {
    return fileData;
   }
  
   public void setFileData(CommonsMultipartFile fileData) {
    this.fileData = fileData;
   }
  }
  </code></pre>
  
  <p>The setFileData() method requires the CommonsMultipartFile object which I'm trying to create just given a file in my src/test/resources directory.</p>
  
  <p>Would anyone know how I can take a file, create a FileItem object and pass that to the CommonsMultipartFile object constructor?</p>
  
  <p>Thanks. If anything is unclear, please let me know - I'm not that familiar with Spring MVC file uploads.</p>