簡體   English   中英

使用 TestRestTemplate 進行多部分 POST 請求的集成測試返回 400

[英]Integration test with TestRestTemplate for Multipart POST request returns 400

我知道類似的問題已經出現了幾次,但遵循建議的修復並沒有解決我的問題。

我有一個帶有以下端點的簡單控制器:

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> singleFileUpload(@RequestParam("file") MultipartFile file) {
    log.debug("Upload controller - POST: {}", file.getOriginalFilename());

    // do something
}

我正在嘗試使用 Spring TestRestTemplate為其編寫集成測試,但我所有的TestRestTemplate都以400 - Bad Request結束(沒有日志說明控制台中出了什么問題)。

控制器內的日志沒有被擊中,所以在到達那里之前它失敗了。

你能看看我的測試並建議我做錯了什么嗎?

@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));
}

我嘗試了以下方法:

@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));
}

正如你所看到的,我使用了org.springframework.core.io.ClassPathResource作為文件的對象,ti 就像一個魅力

我希望它有用

安傑洛

如果您想使用java.nio.file.Path也可以使用FileSystemResource

包: org.springframework.core.io.FileSystemResource

例如,你可以這樣做:

new FileSystemResource(Path.of("src", "test", "resources", "image.jpg"))

完整代碼示例:

@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"));
    }
}

休息控制器:

@RestController
public class UploadEndpoint {
    @PostMapping("/upload")
    public void uploadFile(@RequestParam("file") MultipartFile file) {
        System.out.println(file.getSize());
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM