使用TestRestTemplate进行Multipart POST请求的集成测试返回400(Integration test with TestRestTemplate for Multipart

编程入门 行业动态 更新时间:2024-10-07 12:25:42
使用TestRestTemplate进行Multipart 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结尾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)); }

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

最满意答案

我尝试了以下方法:

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

我希望它有用

安杰洛

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

更多推荐

本文发布于:2023-08-07 12:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464134.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   POST   TestRestTemplate   Multipart   request

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!