I have a problem about writing a test to upload Image in Amazon s3 in Spring Boot.
I tried to write its test method but I got an error shown below.
How can I fix it?
Here is the method of Rest controller
@RestController @RequestMapping("/api/v1/bookImage") @RequiredArgsConstructor public class ImageRestController { @PostMapping public ResponseEntity<String> uploadImage(@RequestParam("bookId") Long bookId, @RequestParam("file") MultipartFile file) { final String uploadImg = imageStoreService.uploadImg(convert(file), bookId); service.saveImage(bookId, uploadImg); return ResponseEntity.ok(uploadImg); } private File convert(final MultipartFile multipartFile) { // convert multipartFile to File File file = new File(Objects.requireNonNull(multipartFile.getOriginalFilename())); try (FileOutputStream fos = new FileOutputStream(file)) { fos.write(multipartFile.getBytes()); return file; } catch (IOException e) { throw new RuntimeException("Failed to convert multipartFile to File"); } }
Here is the method of imageService.
public String uploadImg(File file, Long bookId) { s3amazon.putObject(BUCKET_NAME, bookId.toString(), file); return baseUrl + bookId; }
Here is the test method shown below.
@Test void itShouldGetImagePath_WhenValidBookIdAndFile() throws Exception{ // given - precondition or setup String bookId = "1"; String imagePath = "amazon-imagepath"; String baseUrl = String.format(imagePath + "/%s", bookId); Long bookIdValue = 1L; MockMultipartFile uploadFile = new MockMultipartFile("file", new byte[1]); // when - action or the behaviour that we are going test when(imageStoreService.uploadImg(convert(uploadFile), bookIdValue)).thenReturn(baseUrl); -> HERE IS THE ERROR // then - verify the output mvc.perform(MockMvcRequestBuilders.multipart("/api/v1/bookImage") .file(uploadFile) .param("bookId", bookId)) .andExpect((ResultMatcher) content().string(baseUrl)) .andExpect(status().isOk()); } private File convert(final MultipartFile multipartFile) { // convert multipartFile to File File file = new File(Objects.requireNonNull(multipartFile.getOriginalFilename())); try (FileOutputStream fos = new FileOutputStream(file)) { fos.write(multipartFile.getBytes()); return file; } catch (IOException e) { throw new RuntimeException("Failed to convert multipartFile to File"); } }
Here is the error : Failed to convert multipartFile to File (java.lang.RuntimeException: Failed to convert multipartFile to Filejava.io.FileNotFoundException: )
How can I fix it?
Advertisement
Answer
Here is the solution shown below.
@Test void itShouldGetImagePath_WhenValidBookIdAndFile() throws Exception{ // given - precondition or setup String bookId = "1"; String imagePath = "amazon-imagepath"; String baseUrl = String.format(imagePath + "/%s", bookId); Long bookIdValue = 1L; String fileName = "sample.png"; MockMultipartFile uploadFile = new MockMultipartFile("file", fileName, "image/png", "Some bytes".getBytes()); // when - action or the behaviour that we are going test when(imageStoreService.uploadImg(convert(uploadFile), bookIdValue)).thenReturn(baseUrl); doNothing().when(bookSaveService).saveImage(bookIdValue,baseUrl); // then - verify the output mvc.perform(MockMvcRequestBuilders.multipart("/api/v1/bookImage") .file(uploadFile) .param("bookId", bookId)) .andDo(print()) .andExpect(status().isOk()) .andExpect(jsonPath("$").value(baseUrl)) .andExpect(content().string(baseUrl)); }