I am trying to write Junit tests for a Controller class which contains the following method.
@RequestMapping(value = "/mappingUrl", method = RequestMethod.POST)
public String uploadFileMethod(HttpServletResponse httpResponse, HttpServletRequest httpRequest, ModelMap model) throws Exception {
try {
MultipartFile multipartFile = ((MultipartHttpServletRequest) httpRequest).getFile("fileName");
}
catch(Exception e){}
}
In the test class I have the following method
@Test
public void testUploadFileMethod() throws Exception {
mockMVC.perform(post("/mappingUrl")).andExpect(status().isOk());
}
I am getting the the below exception when the test is executed:
java.lang.ClassCastException: org.springframework.mock.web.MockHttpServletRequest cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest
Is there a way I can test the method without changing the existing code? The class is used through out the application and I am afraid I might break something else.
I went through questions that were similar and the following are the ones that came close:
Mockito ClassCastException - A mock cannot be cast
pass remoteUser value in HttpServletRequest to mockmvc perform test