我使用 Spring Boot 创建一个文件上传服务,并使用 Spring Mock Mvc 和 MockMultipartFile 对其进行测试。我想测试当超过最大文件大小时是否抛出错误。以下测试失败,因为它收到 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());
是否可以测试上传文件大小?
最佳答案
根据documentation :
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);
关于spring - 使用 MockMultipartFile 测试最大上传文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33667276/