本文介绍了集成测试用例和文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了一些与使用 spring 上传文件相关的代码,它工作正常,现在我正在为此编写集成测试用例,但我面临一些问题我的控制器方法,
I wrote some code for related to upload a file using spring, It works fine, Now I am writing integration test cases for that but I am facing some issue My controller method,
@RequestMapping(value = "/{attributeName}/upload", method = RequestMethod.POST)
@ResponseBody
public Result uploadCompany(HttpServletRequest request,
@RequestParam MultipartFile file, @PathVariable String attributeName,
@RequestParam long dateKey)
throws IOException, PromotionException {
some code
}
测试用例
@Test
public void shouldReturnTrueStatusWhenUploadCompany() throws Exception {
MockMultipartFile file = new MockMultipartFile("company_upload", "company_upload.csv",
MediaType.MULTIPART_FORM_DATA_VALUE, EMPLOYEE_NUMBER_FILE_CONTENT.getBytes(UTF_8));
mockMvc.perform(
MockMvcRequestBuilders.fileUpload(
PROMOTION + StringUtils.replace(ATTRIBUTE_NAME, "{attributeName}", "COMPANY") + "/upload")
.file(file).param("dateKey", "852017") .contentType(MediaType.MULTIPART_FORM_DATA)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
但是我得到了
2017-05-09 13:42:42,506 ERROR [Test worker] INTERNAL_SERVER_ERROR:
org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'file' is not present
我哪里错了?
推荐答案
换行
MockMultipartFile file = new MockMultipartFile("company_upload", "company_upload.csv",
MediaType.MULTIPART_FORM_DATA_VALUE, EMPLOYEE_NUMBER_FILE_CONTENT.getBytes(UTF_8));
到
MockMultipartFile file = new MockMultipartFile("file", "company_upload.csv",
MediaType.MULTIPART_FORM_DATA_VALUE, EMPLOYEE_NUMBER_FILE_CONTENT.getBytes(UTF_8));
或者把你的控制器方法声明改成这样
or change your controller method declaration to something like this
public Result uploadCompany(HttpServletRequest request,
@RequestParam(value = "company_upload") MultipartFile file, @PathVariable String attributeName,
@RequestParam long dateKey)
这篇关于集成测试用例和文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!