使用Spring MVCtesting来unit testing多部分POST请求

我有以下请求处理程序来保存自动。 我已经证实,这个工程,当我使用如cURL。 现在我想用Spring MVC Test对这个方法进行unit testing。 我曾尝试使用fileUploader,但我没有pipe理得到它的工作。 我也没有设法添加JSON部分。

我将如何unit testing这个方法与Spring MVCtesting? 我无法find任何这方面的例子。

@RequestMapping(value = "autos", method = RequestMethod.POST) public ResponseEntity saveAuto(@RequestPart(value = "data") AutoResource, @RequestParam(value = "files[]", required = false) List<MultipartFile> files) {...} 

我想为我的自动+一个或多个文件uplod一个JSON表示。

我将在赏金中加100个正确的答案!

使用MockMvcRequestBuilders#fileUpload(String, Object...) ,它返回一个MockMultipartHttpServletRequestBuilder 。 然后链接一堆file(MockMultipartFile)调用。

这是一个工作的例子。 给定一个@Controller

 @Controller public class NewController { @RequestMapping(value = "/upload", method = RequestMethod.POST) @ResponseBody public String saveAuto( @RequestPart(value = "json") JsonPojo pojo, @RequestParam(value = "some-random") String random, @RequestParam(value = "data", required = false) List<MultipartFile> files) { System.out.println(random); System.out.println(pojo.getJson()); for (MultipartFile file : files) { System.out.println(file.getOriginalFilename()); } return "success"; } static class JsonPojo { private String json; public String getJson() { return json; } public void setJson(String json) { this.json = json; } } } 

和一个unit testing

 @WebAppConfiguration @ContextConfiguration(classes = WebConfig.class) @RunWith(SpringJUnit4ClassRunner.class) public class Example { @Autowired private WebApplicationContext webApplicationContext; @Test public void test() throws Exception { MockMultipartFile firstFile = new MockMultipartFile("data", "filename.txt", "text/plain", "some xml".getBytes()); MockMultipartFile secondFile = new MockMultipartFile("data", "other-file-name.data", "text/plain", "some other type".getBytes()); MockMultipartFile jsonFile = new MockMultipartFile("json", "", "application/json", "{\"json\": \"someValue\"}".getBytes()); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); mockMvc.perform(MockMvcRequestBuilders.fileUpload("/upload") .file(firstFile) .file(secondFile).file(jsonFile) .param("some-random", "4")) .andExpect(status().is(200)) .andExpect(content().string("success")); } } 

@Configuration

 @Configuration @ComponentScan({ "test.controllers" }) @EnableWebMvc public class WebConfig extends WebMvcConfigurationSupport { @Bean public MultipartResolver multipartResolver() { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); return multipartResolver; } } 

testing应该通过并给你输出

 4 // from param someValue // from json file filename.txt // from first file other-file-name.data // from second file 

需要注意的是,您发送JSON的方式与其他任何多部分文件一样,除了使用不同的内容types。

看看这个取自Spring MVC展示的例子,这是链接到源代码 :

 @RunWith(SpringJUnit4ClassRunner.class) public class FileUploadControllerTests extends AbstractContextControllerTests { @Test public void readString() throws Exception { MockMultipartFile file = new MockMultipartFile("file", "orig", null, "bar".getBytes()); webAppContextSetup(this.wac).build() .perform(fileUpload("/fileupload").file(file)) .andExpect(model().attribute("message", "File 'orig' uploaded successfully")); } }