我使用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:
原文链接:https://www.f2er.com/spring/432267.htmlIf 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",bytes);