使用MockMultipartFile测试最大上传文件大小 [英] Test maximum upload file size with MockMultipartFile

查看:2761
本文介绍了使用MockMultipartFile测试最大上传文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring Boot创建文件上传服务,并使用Spring Mock Mvc和MockMultipartFile对其进行测试.我想测试超过最大文件大小时是否引发错误.以下测试失败,因为它收到200.

I create a file upload service with Spring Boot and test it with Spring Mock Mvc and MockMultipartFile. I want to test if an error is thrown when the maximum file size is exceeded. The following test fails because it receive a 200.

RandomAccessFile f = new RandomAccessFile("t", "rw");
f.setLength(1024 * 1024 * 10);
InputStream is = Channels.newInputStream(f.getChannel());

MockMultipartFile firstFile = new MockMultipartFile("data", "file1.txt", "text/plain", is);

mvc.perform(fileUpload("/files")
    .file(firstFile))
    .andExpect(status().isInternalServerError());

是否可以测试上传文件的大小?

Is there any possibility to test the upload file size?

推荐答案

根据

如果使用length方法返回的文件的当前长度为 小于newLength参数,则文件将被扩展.在 在这种情况下,文件扩展部分的内容不是 定义.

If the present length of the file as returned by the length method is smaller than the newLength argument then the file will be extended. In this case, the contents of the extended portion of the file are not defined.

尝试以下方法:

byte[] bytes = new byte[1024 * 1024 * 10];
MockMultipartFile firstFile = new MockMultipartFile("data", "file1.txt", "text/plain", bytes);

这篇关于使用MockMultipartFile测试最大上传文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆