CLOSE FULL VIEW
Spring 3.1 and MVC Testing Support
Recorded at:
Community comments
Test a uploading file Controller
by
Alberto García Rubio
Posted
Test a uploading file Controller
by
Alberto García Rubio
Congratulations for the presentation. Very interesting and useful.
I am trying to test a controller with the spring-mvc-test but I cannot get it work correctly. The functionality of my controller is to upload a file to a server, so it has as method parameter a class embendding a CommonsMultiparFile. This is the signature of the upload method
@RequestMapping("/documentManager/createOperationDocument")
public @ResponseBody
String createOperationDocument(HttpServletRequest request,@RequestParam String name,@RequestParam String description,FileUploadBean uploadItem){}
The Class FileUploadBean is the one embedding the commons CommonsMultipartFile:
public class FileUploadBean {
private CommonsMultipartFile file;
public CommonsMultipartFile getFile() {
return file;
}
public void setFile(CommonsMultipartFile file) {
this.file = file;
}
}
I am testing this method following the tips in this presentation:
MockMvc mockMvc = xmlConfigSetup("classpath:*.xml").build();
MultipartRequestBuilder mrb = fileUpload("/documentManager/createOperationDocument");
mrb.contentType(MediaType.MULTIPART_FORM_DATA);
mrb.param("description", "descripcion test.pdf");
mrb.param("name", "test.pdf");
byte[] data = {1,2,3,4,5};
MockMultipartFile file = new MockMultipartFile("test.pdf", data);
mrb.file(file);
ResultActions resultActions = mockMvc.perform(mrb);
When Executing the test, the controller method "createOperationDocument" is called but the parameter "FileUploadBean" is always null.
Any idea whats happening? From the web application this method is called and it works fine, the "FileUploadBean" is no null and contains the file uploaded.
Regards,
Alberto
I am trying to test a controller with the spring-mvc-test but I cannot get it work correctly. The functionality of my controller is to upload a file to a server, so it has as method parameter a class embendding a CommonsMultiparFile. This is the signature of the upload method
@RequestMapping("/documentManager/createOperationDocument")
public @ResponseBody
String createOperationDocument(HttpServletRequest request,@RequestParam String name,@RequestParam String description,FileUploadBean uploadItem){}
The Class FileUploadBean is the one embedding the commons CommonsMultipartFile:
public class FileUploadBean {
private CommonsMultipartFile file;
public CommonsMultipartFile getFile() {
return file;
}
public void setFile(CommonsMultipartFile file) {
this.file = file;
}
}
I am testing this method following the tips in this presentation:
MockMvc mockMvc = xmlConfigSetup("classpath:*.xml").build();
MultipartRequestBuilder mrb = fileUpload("/documentManager/createOperationDocument");
mrb.contentType(MediaType.MULTIPART_FORM_DATA);
mrb.param("description", "descripcion test.pdf");
mrb.param("name", "test.pdf");
byte[] data = {1,2,3,4,5};
MockMultipartFile file = new MockMultipartFile("test.pdf", data);
mrb.file(file);
ResultActions resultActions = mockMvc.perform(mrb);
When Executing the test, the controller method "createOperationDocument" is called but the parameter "FileUploadBean" is always null.
Any idea whats happening? From the web application this method is called and it works fine, the "FileUploadBean" is no null and contains the file uploaded.
Regards,
Alberto




Hello stranger!
You need to Register an InfoQ account or Login to post comments. But there's so much more behind being registered.Get the most out of the InfoQ experience.
Tell us what you think