java—在mockmvc post请求中包含reguestpart

1dkrff03  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(366)

使用spring boot版本:1.5.17.release在使用mockmvc测试带有requestpart的终结点时获取异常:

/**
 * POST /categories
 */
public ResponseEntity categoryPost(
        @ApiParam(value = ""  ) @RequestPart(value = "category") String categoryFormString,
        @ApiParam(value = "") @RequestPart(value = "image", required = false) MultipartFile image)
        throws IOException {

我正在使用mockmvc测试此控制器,如下所示:

HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.add("Content-Type", "multipart/form-data");
    httpHeaders.add("Authorization", "Bearer 455a2d79-d83a-0000-aa8b-0000000");

    MockMultipartFile categoryPart =
            new MockMultipartFile(
                    "category",
                    categoryFormJson.getBytes());

    MvcResult result = mockMvc.perform(MockMvcRequestBuilders.fileUpload("/categories")
            .file(categoryPart)
            .headers(httpHeaders))
            .andReturn();

    Assert.assertNull(result.getResponse().getErrorMessage());

运行测试时,出现以下异常:

java.lang.AssertionError: expected null, but was:<Required request part 'category' is not present>

有什么提示我在这里遗漏了什么吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题