英文:
How to write Integration Test for Rest Post Api MultiPart using ClientBuilder
问题
我正在尝试为多部分上传的 Post API 编写集成测试,但无法成功。我的代码如下所示:
我的控制器方法如下所示:
create(@RequestPart("createBean") CreateBean bean,
@RequestPart("infoBean") InfoBean infobean,
@RequestPart(value="file" required=false) List<MultiPartFile> file){}
为了对这个 Post API 进行集成测试,我正在尝试以下操作:
ClientBuilder.newClient().target(LocalHost).request().post(Entity.entity(bean), MediaType.MULTIPART_FORM_DATA);
但是出现了错误。
我应该如何将 bean、infobean 和 file 作为单个请求的一部分进行传递呢?
英文:
I am trying to write Integration test for Post api for Multipart upload but unable to so.
My cod looks like this..
My controller method looks like below.
create(@RequestPart("createBean") CreateBean bean,
@RequestPart("infoBean") InfoBean infobean,
@RequestPart(value="file" required=false) List<MultiPartFile> file){}
For integration test of this Post Api I am trying to do the following.
ClientBuilder.newClient().target(LocalHost).request().post(Entity.entity(bean),MediaType.MULTIPART_FORM_DATA);
But getting error.
How can I pass bean , infobean and file as part of single request.
答案1
得分: 1
写 Multipart 的集成测试,你只需使用 FormDataMultipart。
只需传递带有名称值对的 bean。
formDataMultipart.field("createBean", bean, 媒体类型.Json)
然后将其作为 Entity.entity(formDataMultipart) 传递。
英文:
To write integration test for Multipart you can just use FormDataMultipart.
Just pass the bean with name value pair.
formDataMultipart.field("createBean", bean, Media type.Json)
and just pass this as Entity.entity(formDataMultipart)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论