【问题标题】:Spring unit test : IllegalStateException for MultipartHttpServletRequestSpring 单元测试:MultipartHttpServletRequest 的 IllegalStateException
【发布时间】:2016-05-23 06:42:37
【问题描述】:

我有一个基于以下方法的单元测试:

@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

知道如何解决上述错误消息吗?

【问题讨论】:

  • 通过正确设置您的测试。您的请求、响应和会话是无用的。那些没有被使用,你所做的设置也是如此。在您的@Before 中,您只需设置MockMvc 即可。接下来您的请求有缺陷,因为它是普通帖子而不是多部分帖子,您应该更改内容类型。
  • 您真的要上传文件还是只想发送两个请求参数“licNo”和“queId”?测试中没有找到任何文件上传代码。

标签: java unit-testing spring-mvc powermock spring-test-mvc


【解决方案1】:

您必须使用MockMvcRequestBuilders.fileUpload() 上传文件来测试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 注解访问请求参数,而不是从HttpServletRequest 访问它们。 Spring 框架会自动为您完成。

例如:

@RequestMapping(value = "/att/handle", method = RequestMethod.POST)
public @ResponseBody
String handleUpload(@RequestParam String licNo, @RequestParam String queId, ...) throws IOException { ... }

【讨论】:

    【解决方案2】:

    您正在测试没有任何文件的文件上传。尝试用mockMvc.perform(fileUpload("/query/att/handle", variables).file("filename", data))测试控制器方法

    API 文档:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/web/servlet/request/MockMvcRequestBuilders.html#fileUpload-java.lang.String-java.lang.Object...-

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 2012-12-22
      • 2016-05-28
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多