Integration test with TestRestTemplate for Multipart POST request returns 400
Asked Answered
A

3

18

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));
}
Attribution answered 13/4, 2017 at 13:38 Comment(4)
It looks like you are setting the ContentType = MULTIPART_FORM_DATA on the test. But your controller is not set to receive this type of request. Try using consumes = {MediaType.MULTIPART_FORM_DATA_VALUE} Please post the rest of the Test class as there are some missing pieces that I couldn't reproduce hereFukuoka
@BrunoLeite Thank you for the suggestion. I tried it but that is not the case.Attribution
do you actually return a string with your response entity? have you verified what's at the body of your 400 response in case it provides more details? have you considered running it with a debugger or implementing an exception handler?Furtado
Yes, String is being returned. It works fine outside of the integration test. In this test I posted. I don't even get the debug line so something is happening before it gets to the body of the controller.Attribution
T
31

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

Triste answered 18/4, 2017 at 16:21 Comment(4)
It is not clear to me what was causing the problem in the first place. Is the multipart controller not compatible with java.io.File? Anyway, it does work like a charm, thank you. This saved me a lot of work!Attribution
@Attribution I guess that the rest template is able in managing the org.springframework.core.it.Resource implementation better than passing a simple java.io.FileTriste
I know this is quite old, but can I ask you, where is your "image.jpg" file located in your project?Hardin
@bot_bot In my code the image is under the classpath (that is in src/main/resources if you are using maven)Triste
A
3

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());
    }
}
Affray answered 13/4, 2021 at 13:29 Comment(0)
W
0

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;
    }
Waddington answered 27/4 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.