Spring 单元测试:MultipartHttpServletRequest 的 IllegalStateException
Spring unit test : IllegalStateException for MultipartHttpServletRequest
我要根据以下方法进行单元测试:
@RequestMapping(value = "/query")
public class QueryController {
...
@RequestMapping(value = "/att/handle", method = RequestMethod.POST)
public @ResponseBody
String handleUpload(MultipartHttpServletRequest request, HttpServletResponse response) throws IOException {
JsonResponseDto responseDto = null;
HashMap<Long, String> attachmentInfoMap = null;
String licNo = request.getParameter("licNo");
String queId = request.getParameter("queId");
....
请在下面找到我的单元测试:
@RunWith(PowerMockRunner.class)
public class QueryControllerTest {
@InjectMocks
private QueryController queryController;
private MockMvc mockMvc;
private MockMultipartHttpServletRequest request;
private MockHttpServletResponse response;
private MockHttpSession session;
@Before
public void setup() {
request = new MockMultipartHttpServletRequest();
request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
response = new MockHttpServletResponse();
session = new MockHttpSession();
request.setSession(session);
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
//Added viewResolver to prevent circular view path error
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
this.mockMvc = MockMvcBuilders.standaloneSetup(queryController).setViewResolvers(viewResolver).build();
}
@Test
public void handleUploadQueryAttachmentsOK() throws Exception {
mockMvc.perform(post("/query/att/handle").param("queId", "123").param("licNo", "12"))
.andExpect(status().isFound()
);
}
当我执行单元测试时,显示以下错误:
java.lang.IllegalStateException: Current request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest]: org.springframework.mock.web.MockHttpServletRequest
知道如何解决上述错误消息吗?
您正在测试没有任何文件的文件上传。尝试使用 mockMvc.perform(fileUpload("/query/att/handle", variables).file("filename", data))
测试控制器方法
您必须使用 MockMvcRequestBuilders.fileUpload() to test MultipartHttpServletRequest 上传文件。
例如:
@Test
public void uploadImage() throws Exception {
String data = "test-data";
MockMultipartFile imageFile = new MockMultipartFile("image", "my-image.jpeg", "image/jpeg", data.getBytes());
mockMvc.perform(MockMvcRequestBuilders.fileUpload("/query/att/handle").file(imageFile).param("queId", "123").param("licNo", "12"));
}
这里是 thread 围绕它的相同讨论。
使用@RequestParam annotation to access request parameter instead of accessing them from HttpServletRequest。 Spring 框架会自动为您完成。
例如:
@RequestMapping(value = "/att/handle", method = RequestMethod.POST)
public @ResponseBody
String handleUpload(@RequestParam String licNo, @RequestParam String queId, ...) throws IOException { ... }
我要根据以下方法进行单元测试:
@RequestMapping(value = "/query")
public class QueryController {
...
@RequestMapping(value = "/att/handle", method = RequestMethod.POST)
public @ResponseBody
String handleUpload(MultipartHttpServletRequest request, HttpServletResponse response) throws IOException {
JsonResponseDto responseDto = null;
HashMap<Long, String> attachmentInfoMap = null;
String licNo = request.getParameter("licNo");
String queId = request.getParameter("queId");
....
请在下面找到我的单元测试:
@RunWith(PowerMockRunner.class)
public class QueryControllerTest {
@InjectMocks
private QueryController queryController;
private MockMvc mockMvc;
private MockMultipartHttpServletRequest request;
private MockHttpServletResponse response;
private MockHttpSession session;
@Before
public void setup() {
request = new MockMultipartHttpServletRequest();
request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
response = new MockHttpServletResponse();
session = new MockHttpSession();
request.setSession(session);
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
//Added viewResolver to prevent circular view path error
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
this.mockMvc = MockMvcBuilders.standaloneSetup(queryController).setViewResolvers(viewResolver).build();
}
@Test
public void handleUploadQueryAttachmentsOK() throws Exception {
mockMvc.perform(post("/query/att/handle").param("queId", "123").param("licNo", "12"))
.andExpect(status().isFound()
);
}
当我执行单元测试时,显示以下错误:
java.lang.IllegalStateException: Current request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest]: org.springframework.mock.web.MockHttpServletRequest
知道如何解决上述错误消息吗?
您正在测试没有任何文件的文件上传。尝试使用 mockMvc.perform(fileUpload("/query/att/handle", variables).file("filename", data))
您必须使用 MockMvcRequestBuilders.fileUpload() to test MultipartHttpServletRequest 上传文件。
例如:
@Test
public void uploadImage() throws Exception {
String data = "test-data";
MockMultipartFile imageFile = new MockMultipartFile("image", "my-image.jpeg", "image/jpeg", data.getBytes());
mockMvc.perform(MockMvcRequestBuilders.fileUpload("/query/att/handle").file(imageFile).param("queId", "123").param("licNo", "12"));
}
这里是 thread 围绕它的相同讨论。
使用@RequestParam annotation to access request parameter instead of accessing them from HttpServletRequest。 Spring 框架会自动为您完成。
例如:
@RequestMapping(value = "/att/handle", method = RequestMethod.POST)
public @ResponseBody
String handleUpload(@RequestParam String licNo, @RequestParam String queId, ...) throws IOException { ... }