我正在尝试测试这个控制器:
@RequestMapping(value="/u",consumes="multipart/form-data", method = RequestMethod.POST)
public @ResponseBody String register(
@RequestParam String u,
@RequestParam CommonsMultipartFile filea,
@RequestParam CommonsMultipartFile fileb,
@RequestParam CommonsMultipartFile filec,
@RequestParam CommonsMultipartFile filed) {
return "hi";
}
使用这个请求的模拟:
mockMvc.perform(
MockMvcRequestBuilders.fileUpload("/u")
.file("filea","id.jpg".getBytes())
.file("fileb","pc.jpg".getBytes())
.file("filec","cl.jpg".getBytes())
.file("filed","fo.jpg".getBytes())
.param("u", u))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print());
虽然,我想我错误地编写了MockMvcRequest,因为测试失败了(返回的状态是500)。
提前致谢。
答案 0 :(得分:1)
问题非常小 - 只需将CommonsMultipartFile
更改为MultipartFile
,您的测试就应该干净利落。
此问题的原因是创建的模拟文件上传参数为MockMultipartFile
,无法转换为更具体的CommonsMultipartFile
类型。
答案 1 :(得分:0)
如何测试分段上传的简单方法是使用StandardServletMultipartResolver。 并且为了测试使用此代码:
final MockPart profilePicture = new MockPart("profilePicture", "stview.jpg", "image/gif", "dsdsdsd".getBytes());
final MockPart userData = new MockPart("userData", "userData", "application/json", "{\"name\":\"test aida\"}".getBytes());
this.mockMvc.perform(
fileUpload("/endUsers/" + usr.getId().toString()).with(new RequestPostProcessor() {
@Override
public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
request.addPart(profilePicture);
request.addPart(userData);
return request;
}
})
MockPart类
public class MockPart extends MockMultipartFile implements Part {
private Map<String, String> headers;
public MockPart(String name, byte[] content) {
super(name, content);
init();
}
public MockPart(String name, InputStream contentStream) throws IOException {
super(name, contentStream);
init();
}
public MockPart(String name, String originalFilename, String contentType, byte[] content) {
super(name, originalFilename, contentType, content);
init();
}
public MockPart(String name, String originalFilename, String contentType, InputStream contentStream) throws IOException {
super(name, originalFilename, contentType, contentStream);
init();
}
public void init() {
this.headers = new HashMap<String, String>();
if (getOriginalFilename() != null) {
this.headers.put("Content-Disposition".toLowerCase(), "form-data; name=\"" + getName() + "\"; filename=\"" + getOriginalFilename() + "\"");
} else {
this.headers.put("Content-Disposition".toLowerCase(), "form-data; name=\"" + getName() + "\"");
}
if (getContentType() != null) {
this.headers.put("Content-Type".toLowerCase(), getContentType());
}
}
@Override
public void write(String fileName) throws IOException {
}
@Override
public void delete() throws IOException {
}
@Override
public String getHeader(String name) {
return this.headers.get(name.toLowerCase());
}
@Override
public Collection<String> getHeaders(String name) {
List<String> res = new ArrayList<String>();
if (getHeader(name) != null) {
res.add(getHeader(name));
}
return res;
}
@Override
public Collection<String> getHeaderNames() {
return this.headers.keySet();
}
}