InfoQ Homepage Presentations Spring 3.1 and MVC Testing Support
Spring 3.1 and MVC Testing Support
Summary
Sam Brannen and Rossen Stoyanchev introduce the TestContext Framework, how to use @Configuration and environment profiles for testing with Spring 3.1, and the testing support available in Spring MVC.
Bio
Sam Brannen is a Senior Software Consultant with over 13 years' experience, co-founder of Swiftmind, former Spring Core and dm Server developer and lead author of Spring in a Nutshell. Rossen Stoyanchev is an engineer for SpringSource, a committer on the Spring MVC and the Spring Web Flow projects, and author of a Spring Web course.
About the conference
SpringOne 2GX is a one-of-a-kind conference for application developers, solution architects, web operations and IT teams who develop business applications, create multi-device aware web applications, design cloud architectures, and manage high performance infrastructure. The sessions are specifically tailored for developers using the hugely popular open source Spring technologies, Groovy & Grails, and Tomcat. Whether you're building and running mission-critical business applications or designing the next killer cloud application, SpringOne 2GX will keep you up to date with the latest enterprise technology.
Community comments
Test a uploading file Controller
by Alberto García Rubio,
Test a uploading file Controller
by Alberto García Rubio,
Your message is awaiting moderation. Thank you for participating in the discussion.
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