[英]Spring Boot: file upload testing
I'm trying to create a Unit test in a Spring REST application.我正在尝试在 Spring REST 应用程序中创建单元测试。 the test is related to an endpoint for MultipartFile uploading.
该测试与 MultipartFile 上传的端点有关。
Here is the arguments that my method accepts within my @RestController
这是我的方法在
@RestController
接受的参数
@PostMapping("/upload")
public ResponseEntity uploadFile(@HasFileName @RequestParam("file") MultipartFile file,
@NotEmpty @RequestParam("entity") String entity, @NotEmpty @RequestParam("language") String language,
@NotEmpty @RequestParam("lastModified") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime lastModifiedDateTime,
@NotEmpty @RequestParam("createdDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime createdDateTime)
throws IOException {
I can't find a way to get this information from the multipart inside the unit test.我找不到从单元测试中的多部分获取此信息的方法。 I can't find documentation on making spring unit test for Spring REST, so any inputs would be appreciated.
我找不到有关为 Spring REST 进行 Spring 单元测试的文档,因此将不胜感激。
@Test
public void testUploadFile() throws Exception{
ResultMatcher ok = MockMvcResultMatchers.status().isOk();
String filename = "test.txt";
File file = new File("/test" + filename);
file.delete();
MockMultipartFile mockMultipartFile = new MockMultipartFile("file", "test.txt", "multipart/form-data", "test data".getBytes());
upload.uploadFile(mockMultipartFile, mockMultipartFile.getName()) //missing more data
//more logic on how to assert that the file is not empty
}
In principle the test should validate the outcome of the uploadFile method():原则上,测试应该验证 uploadFile 方法()的结果:
ResultActions result =
this.mockMvc.perform(multipart("/svc").file(file)
.header(HttpHeaders.AUTHORIZATION,
"Bearer token"))
.andExpect(content().string("response"))
.andExpect(status().isOk());
The file is an input parameter so I dont understand why you want to verify it is not empty: it could be useful I believe to verify the interactions with other mocks (for instance checking the file is used in a FileService.save() method)该文件是一个输入参数,所以我不明白您为什么要验证它不为空:我相信验证与其他模拟的交互可能很有用(例如检查文件是否在 FileService.save() 方法中使用)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.