我使用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@H_403_19@ smaller than the newLength argument then the file will be extended. In@H_403_19@ this case,the contents of the extended portion of the file are not@H_403_19@ defined.
试试这个:
byte[] bytes = new byte[1024 * 1024 * 10];
MockMultipartFile firstFile = new MockMultipartFile("data",bytes);