使用TestRestTemplate进行Multipart POST请求的集成测试返回400

时间:2017-04-13 13:39:00

标签: java spring spring-boot junit resttemplate

我知道类似的问题已经在这里出现了几次,但是根据建议的修复并没有解决我的问题。

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

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

    // do something
}

我正在尝试使用Spring 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));
}

2 个答案:

答案 0 :(得分:7)

我尝试了以下内容:

@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就像魅力一样

我希望它有用

安吉洛

答案 1 :(得分:0)

如果您想使用 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());
    }
}