java - ServletFileUpload.parseRequest() 为 MockMultipartHttpServletRequest 返回一个空列表

标签 java unit-testing file-upload mocking

查看了该主题的一些现有答案后,我仍然无法弄清楚我可能做错了什么......

我的单元测试中有以下请求声明:

final MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest();
    mockRequest.setMethod("POST");
    mockRequest.setContentType(contentType);
    mockRequest.setRequestURI("/upload");
    mockRequest.addParameter("test_param", "test_value");
    mockRequest.addFile(new MockMultipartFile("file1", "test_upload1.txt", "text/plain", "fileContent1".getBytes()));
    mockRequest.addFile(new MockMultipartFile("file2", "test_upload2.txt", "text/plain", "fileContent2".getBytes()));
    mockRequest.setContent("dummyContent".getBytes());

然后我使用另一个类来处理:

public void processServletRequest(final HttpServletRequest request) {

if (ServletFileUpload.isMultipartContent(request)) {

    final FileItemFactory factory = new DiskFileItemFactory();
    final ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = null;

    try {
      items = upload.parseRequest(request);
    } catch (final FileUploadException e) {
        this.logger.error("Could not upload document", e);
        return;
    }

}

但是 upload.parseRequest(request) 总是返回一个空列表。

这两个地方之间没有其他地方可以读取请求的输入流(没有调用 getParameter() 或类似的东西)。

看不出问题是出在单元测试和模拟请求上,还是出在处理过程中……我们将不胜感激。

编辑:好的,手动测试似乎成功了,这意味着我希望 servlet 工作正常,问题在于我构建模拟请求的方式 - 然而,将它与我见过的其他示例进行比较在这里和那里,我真的看不出有什么问题。 isMultipartContent() 测试成功的事实也告诉我,我没有做错完全...

也许我应该使用其他东西来构建虚拟请求,而不是这个 Spring 帮助程序类?

EDIT2:好吧,我放弃了,决定手动构建我的请求内容,通过构建 StringBuilder 添加参数和文件元数据。如果有人能弄清楚我之前做错了什么,我会保持开放状态,但至少我的测试现在可以正常运行。

EDIT3:根据评论中的要求添加最近答案中的代码片段。

public void createMultipartFormDataRequest(MockHttpServletRequest request, String resourceName, String partName) throws IOException {

  // Load resource being uploaded
  byte[] fileContent =      FileCopyUtils.copyToByteArray(getClass().getResourceAsStream(resourceName));

  // Create part & entity from resource
  Part[] parts = new Part[] { new FilePart(partName, new ByteArrayPartSource(resourceName, fileContent)) };
  MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new PostMethod().getParams());

  // Serialize request body
  ByteArrayOutputStream requestContent = new ByteArrayOutputStream();
  multipartRequestEntity.writeRequest(requestContent);

  // Set request body to HTTP servlet request
  request.setContent(requestContent.toByteArray());

  // Set content type to HTTP servlet request (important, includes Mime boundary string)
  request.setContentType(multipartRequestEntity.getContentType());
}

最佳答案

关于java - ServletFileUpload.parseRequest() 为 MockMultipartHttpServletRequest 返回一个空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17325008/

相关文章:

java - 正则表达式仅在字符串之后匹配重复模式

unit-testing - 获取 TypeScript 类的实例?

android - 运行 Spek 测试显示错误 "Empty test suite"

PHPExcel 阅读器——需要帮助

javascript - 在fileuploader.js中,是否可以获取上传文件的父文件夹名称?

java - 多对多映射 Hibernate 出现 ConstraintViolationException

java - SelectionKey iterator.remove() 抛出 UnsupportedOperationException 和无限循环

java - 使用正则表达式屏蔽以下格式

unit-testing - Orbeon 中有单元测试框架吗?

asp.net - 将上传的文件作为 BLOB 存储在 DB 中或有限制的文件夹中,哪里更好?