Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create CommonsMultipartFile object given only a file

I have a junit test method that takes a CommonsMultipartFile object as a parameter.

I'm trying to create a FileItem object so I can pass it to the constructor,

CommonsMultipartFile(org.apache.commons.fileupload.FileItem fileItem)

To do that, I'm trying to create the FileItem object using the DiskFileItem constructor,

DiskFileItem(java.lang.String fieldName, java.lang.String contentType, boolean isFormField, java.lang.String fileName, int sizeThreshold, java.io.File repository)

but I'm not sure how to pass any of those parameters.

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,

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;
 }
}

The setFileData() method requires the CommonsMultipartFile object which I'm trying to create just given a file in my src/test/resources directory.

Would anyone know how I can take a file, create a FileItem object and pass that to the CommonsMultipartFile object constructor?

Thanks. If anything is unclear, please let me know - I'm not that familiar with Spring MVC file uploads.

like image 781
wsams Avatar asked Apr 29 '10 21:04

wsams


People also ask

How do I make a multipart file serializable?

Your only solution is to get rid of the multipartfile field entirely and replace it with something that is serializable (such as a list of strings, a byte array, etc), or write your own code to produce a sequence of bytes such that you can restore this MultipartFile back from that sequence of bytes later.

What is Commonsmultipartfile in Java?

Method SummaryReturn the contents of the file as an array of bytes. java.lang.String. getContentType() Return the content type of the file.


1 Answers

Use the more common interface org.springframework.web.multipart.MultipartFile. instead of org.springframework.web.multipart.commons.CommonsMultipartFile in your Command (UploadItem). (CommonsMultipartFile is a 1:1 implementation of the Interface).

Now you can create an instance of CommonsMultipartFile with the mock class org.springframework.mock.web.MockMultipartFile. (which is element of spring-test.jar).

Then the creation of an MultipartFile in the tests is only one statement, without any cast:

MockMultipartFile mockMultipartFile = new MockMultipartFile(
       "test.txt",                //filename
       "Hallo World".getBytes()); //content
like image 171
Ralph Avatar answered Sep 24 '22 23:09

Ralph