如何解决Spring编写单元测试以上传REST控制器的文件
我正在尝试为我的Rest Controller编写测试用例
@Test
public void anonymousUploadControllerTest() throws Exception {
MockMultipartFile mockMultipartFile = new MockMultipartFile("file","test-file.txt","text/plain","sample data1".getBytes());
mockMvc.perform(MockMvcRequestBuilders.multipart("/upload")
.file(mockMultipartFile))
.andExpect(MockMvcResultMatchers.status().is(200))
.andExpect((ResultMatcher) content().string("File has been uploaded"));
}
返回:
MockHttpServletRequest:
HTTP Method = POST
Request URI = /upload
Parameters = {}
Headers = [Content-Type:"multipart/form-data;charset=UTF-8"]
Body = null
Session Attrs = {}
Handler:
Type = org.sharefiles.root.controllers.UploadController
Method = org.sharefiles.root.controllers.UploadController#uploadFile(MultipartFile)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 500
Error message = null
Headers = [Content-Type:"application/json",X-Content-Type-Options:"nosniff",X-XSS-Protection:"1; mode=block",Cache-Control:"no-cache,no-store,max-age=0,must-revalidate",Pragma:"no-cache",Expires:"0",X-Frame-Options:"DENY"]
Content type = application/json
Body = {"httpStatus":"INTERNAL_SERVER_ERROR","responseMessage":"Something went wrong with uploading your file!","fileAccessLink":null,"authenticated":false}
Forwarded URL = null
Redirected URL = null
Cookies = []
java.lang.AssertionError: Response status
Expected :200
Actual :500
您能帮我找出错误的地方吗? 当我为Service撰写文章时,一切正常。
github上有完整的代码: https://github.com/group-project-keeper/share-files-project/blob/master/src/test/java/org/sharefiles/root/uploadTests/uploadTest.java
致谢,如果有帮助,谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。