我想上传一个多部分文件。当我使用我的REST请求时,一切正常。当我想在JUnit中测试时,我认为不传输multipartfile参数(因为我无法在调试器中到达主断点)。
错误日志:
预期的MultipartHttpServletRequest:是否配置了MultipartResolver?
弹簧共的test.xml :
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- max upload size in bytes -->
<property name="maxUploadSize" value="20971520" /> <!-- 20MB -->
<!-- max size of file in memory (in bytes) -->
<property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->
</bean>
控制器
@RequestMapping(value = "/services/tasks/addDocument", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public String set(@RequestParam("idTask") Long idTask,@RequestParam("descriere") String descriere,@RequestParam("name") String name, @RequestParam("file") MultipartFile file){
//things}
测试
@Test
public void testAddDocController() throws Exception {
FileInputStream fis = new FileInputStream("C:/HaxLogs.txt");
MockMultipartFile multipartFile = new MockMultipartFile("file", fis);
HashMap<String, String> contentTypeParams = new HashMap<String, String>();
contentTypeParams.put("boundary", "265001916915724");
MediaType mediaType = new MediaType("multipart", "form-data", contentTypeParams);
String resp = mockMvc.perform(post("/services/tasks/addDocument").param("idTask", "141").param("descriere","vadfadfad").param("name", "C:/HaxLogs.txt").content(multipartFile.getBytes()).contentType(mediaType))
.andReturn().getResponse().getContentAsString();
//.andExpect(status().isOk());
System.out.println(resp);
}