spring – 使用MockMultipartFile测试最大上载文件大小

前端之家收集整理的这篇文章主要介绍了spring – 使用MockMultipartFile测试最大上载文件大小前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我使用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);

猜你在找的Spring相关文章