【问题标题】:Test a Spring multipart/form-data controller with uploading of some files通过上传一些文件来测试 Spring multipart/form-data 控制器
【发布时间】:2014-04-15 00:19:26
【问题描述】:

我正在尝试测试这个控制器:

@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";
}

这个模拟请求:

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());

虽然,我想我写错了 MockMvcRequest 因为测试失败(返回的状态是 500)。

提前致谢。

【问题讨论】:

    标签: spring spring-mvc multipartform-data spring-test spring-mvc-test


    【解决方案1】:

    这个问题非常小 - 只需将您的 CommonsMultipartFile 更改为 MultipartFile,您的测试就会顺利运行。

    出现此问题的原因是创建的模拟文件上传参数是 MockMultipartFile,无法转换为更具体的 CommonsMultipartFile 类型。

    【讨论】:

      【解决方案2】:

      测试分段上传的简单方法是使用 StandardServletMultipartResolver。 并用于测试使用此代码:

          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 类

      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();
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-28
        • 2016-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-27
        相关资源
        最近更新 更多