【发布时间】:2020-01-14 21:10:28
【问题描述】:
我正在尝试在 Spring REST 应用程序中创建单元测试。该测试与 MultipartFile 上传的端点有关。
这是我的方法在@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 {
我找不到从单元测试中的多部分获取此信息的方法。我找不到有关为 Spring REST 进行弹簧单元测试的文档,因此我们将不胜感激。
@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
}
【问题讨论】:
标签: java spring spring-boot