我知道类似的问题已经在这里已经好几次,但是根据建议的修复并没有解决我的问题。
我有一个简单的控制器,具有以下端点:
@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
更多推荐
发布评论