I am trying to write integration tests for my Post endpoint. This is part of a 2 part process where the Public endpoint feeds into the service endpoint which does the core of the work. This is the public endpoint so the response is mocked.
I am using springMVC and my code works fine, however when trying to use MockMvc.perform to do the post with content i seem to be getting a response with 'Required request part "file" is not present'.
In the code I use HttpEntity to send the file through to the service and that works, I can't see that working here though.
any help would be greatly appreciated.
@PostMapping("/save/image")
public ResponseEntity saveImage(@RequestParam("file") MultipartFile image)
{
// functionality
}
You should have posted the test code you already have so that we could take a look at it. But anyway, here's an example how to test file upload under MockMvc.
File file = new File("path/to/file.jpg");
MockMultipartFile upload = new MockMultipartFile("file", "file.jpg",
MediaType.IMAGE_JPEG_VALUE,
Files.readAllBytes(file.toPath()));
mockMvc.perform(multipart("/save/image").file(upload)).andExpect(status().isNoContent());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With