I’ve a Spring (3.0) Controller with a method which has HttpServletRequest as one of the parameters, since it’s handling (multiple) file uploads.
@RequestMapping(value = "/classified/{idClassified}/dealer/{idPerson}/upload",
method = RequestMethod.POST)
@ResponseBody
public final String uploadClassifiedPicture(
@PathVariable int idClassified,
@PathVariable int idPerson,
@RequestParam String token,
HttpServletRequest request);
How to Unit Test it? I know I can create a MockHttpServletRequest, but I don’t know how to pass one or more files to it.
MockHttpServletRequest request = new MockHttpServletRequest("POST",
"/classified/38001/dealer/54/upload?token=dfak241adf");
I recommend to change the method signature a bit, to make the uploaded file a normal parameter (of type
MultipartFile(notCommonsMultipartFile)):Then you can use a
MockMultipartFilein your test:If you do not want to change the method signature, then you can use
MockMultipartHttpServletRequestinstead.It has a method
addFile(MultipartFile file). And of course the required parameter can be aMockMultipartFile.