Reputation: 1489
I am trying to test this controller:
@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";
}
Whit this mock of a request:
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());
Although, I guess I am writing the MockMvcRequest wrongly because the test fails (the status returned is 500).
Thanks in advance.
Upvotes: 2
Views: 6028
Reputation: 39
The simple way how to test multipart upload is use StandardServletMultipartResolver. and for test use this code:
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 class
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();
}
}
Upvotes: 0
Reputation: 49915
The issue is a very small one - just change your CommonsMultipartFile
to MultipartFile
and your test should run through cleanly.
The reason for this issue is the mock file upload parameter that is created is a MockMultipartFile
which cannot be cast to the more specific CommonsMultipartFile
type.
Upvotes: 1