如何编写使用ClientBuilder的Rest Post Api MultiPart集成测试

huangapple go评论78阅读模式
英文:

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(&quot;createBean&quot;) CreateBean bean,
       @RequestPart(&quot;infoBean&quot;) InfoBean infobean,
       @RequestPart(value=&quot;file&quot; required=false) List&lt;MultiPartFile&gt; 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)

huangapple
  • 本文由 发表于 2020年10月1日 16:02:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64151281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定