与 TestRest 模板的集成测试,用于多部分 POST 请求返回 400

2022-09-03 00:36:30

我知道类似的问题已经在这里出现过几次了,但遵循建议的修复程序并没有解决我的问题。

我有一个具有以下端点的简单控制器:

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

    // do something
}

我正在尝试使用Spring为它编写集成测试,但我的所有尝试都以(没有日志澄清控制台中出了什么问题)结束。TestRestTemplate400 - 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));
}

答案 1

我尝试了以下方法:

@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 对象,ti 就像一个 charm 一样工作org.springframework.core.io.ClassPathResource

我希望它是有用的

安杰洛


答案 2

FileSystemResource如果你想使用,也可以使用。java.nio.file.Path

包: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());
    }
}

推荐