Reputation: 7995
I know that similar question has been here already couple of times but following suggested fixes did not solve my problem.
I have a simple controller with the following endpoint:
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> singleFileUpload(@RequestParam("file") MultipartFile file) {
log.debug("Upload controller - POST: {}", file.getOriginalFilename());
// do something
}
I am trying to write an integration test for it using Spring TestRestTemplate
but all of my attemps end with 400 - Bad Request
(no logs clarifying what went wrong in console).
The log inside the controller did not get hit so it failed before getting there.
Could you please take a look on my test and suggest what am I doing wrong?
@Test
public void testUpload() {
// simulate multipartfile upload
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("image.jpg").getFile());
MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<String, Object>();
parameters.add("file", file);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<MultiValueMap<String, Object>>(parameters, headers);
ResponseEntity<String> response = testRestTemplate.exchange(UPLOAD, HttpMethod.POST, entity, String.class, "");
// Expect Ok
assertThat(response.getStatusCode(), is(HttpStatus.OK));
}
Upvotes: 18
Views: 11359
Reputation: 387
Since MockMultipartFile
inherits from MultipartFile
, we have access to getResource()
method. My implementation as below:
I constructed an array of MockMultipartFiles
@NotNull
private static MockMultipartFile[] mockMultipartFiles() {
Path path = Paths.get("src/test/resources/uploads/");
assertTrue(Files.exists(path));
File dir = new File(path.toUri());
assertNotNull(dir);
File[] files = dir.listFiles();
assertNotNull(files);
return Arrays.stream(files).map(file -> {
try {
return new MockMultipartFile(
file.getName(),
file.getName(),
Files.probeContentType(file.toPath()),
Files.readAllBytes(file.toPath())
);
} catch (IOException ignored) {
throw new CustomServerError("unable to convert files in %s to a file".formatted(path.toString()));
}
})
.toArray(MockMultipartFile[]::new);
}
Add MockMultipartFiles into LinkedMultiValueMap
as Resource
@NotNull
public static MultiValueMap<String, Object> mockMultiPart(String dto) {
MultiValueMap<String, Object> multipart = new LinkedMultiValueMap<>();
// add image files to request
for (var resource : mockMultipartFiles()) {
multipart.add("files", resource.getResource());
}
// create dto
HttpHeaders metadataHeaders = new HttpHeaders();
metadataHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
multipart.add("dto", new HttpEntity<>(dto, metadataHeaders));
return multipart;
}
Upvotes: 0
Reputation: 3466
FileSystemResource
also could be used in case if you want to use java.nio.file.Path
.
Package: org.springframework.core.io.FileSystemResource
For example, you could do this:
new FileSystemResource(Path.of("src", "test", "resources", "image.jpg"))
Full code example:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UploadFilesTest {
private final TestRestTemplate template;
@Autowired
public UploadFilesTest(TestRestTemplate template) {
this.template = template;
}
@Test
public void uploadFileTest() {
var multipart = new LinkedMultiValueMap<>();
multipart.add("file", file());
final ResponseEntity<String> post = template.postForEntity("/upload", new HttpEntity<>(multipart, headers()), String.class);
assertEquals(HttpStatus.OK, post.getStatusCode());
}
private HttpHeaders headers() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
return headers;
}
private FileSystemResource file() {
return new FileSystemResource(Path.of("src", "test", "resources", "image.jpg"));
}
}
Rest controller:
@RestController
public class UploadEndpoint {
@PostMapping("/upload")
public void uploadFile(@RequestParam("file") MultipartFile file) {
System.out.println(file.getSize());
}
}
Upvotes: 3
Reputation: 6954
I tried the following:
@Test
public void testUpload() {
LinkedMultiValueMap<String, Object> parameters = new LinkedMultiValueMap<String, Object>();
parameters.add("file", new org.springframework.core.io.ClassPathResource("image.jpg"));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity<LinkedMultiValueMap<String, Object>>(parameters, headers);
ResponseEntity<String> response = testRestTemplate.exchange(UPLOAD, HttpMethod.POST, entity, String.class, "");
// Expect Ok
assertThat(response.getStatusCode(), is(HttpStatus.OK));
}
As you can see I used the org.springframework.core.io.ClassPathResource
as object for the file and ti worked like a charm
I hope it's useful
Angelo
Upvotes: 31