Test maximum upload file size with MockMultipartFile

最后都变了- 提交于 2019-12-12 10:39:10

问题


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?


回答1:


According to the 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.

Try this instead:

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


来源:https://stackoverflow.com/questions/33667276/test-maximum-upload-file-size-with-mockmultipartfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!