WebTestClient不支持get()方法和requestBody

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

WebTestClient with get() and requestBody not available

问题

我有一个应用程序,其中一个API使用Get方法定义。它还期望请求正文,然后将其映射到POJO。我正在尝试使用webTestClient测试此控制器。但是我没有看到在get()方法中发送请求正文的选项。不确定是否以正确的方式定义了我的webTestClient。

我的控制器如下:

@GetMapping
public Flux<ResponseBody> getAllResources(@RequestBody Resource resource) {
//相关代码在这里...
}

我的测试方法:

@Test
public void myTest() {

webClient.get()
.uri("uri_path")
.header("Content-Type", "application/json")
.accept("application/json")
.exchange()
.expectedStatus.is2xxxSuccessful();
}

我认为既然在控制器中允许使用get调用将对象绑定到POJO,应该有一些方法可以使用webTestClient进行测试。

英文:

I have an application where one of the api is defined with Get method. It also expects request body which then gets mapped to POJO. I am trying to test this controller using webTestClient. But I do not see an option to send request body with get() method. Not sure if I am defining my webTestClient in the right way.

My controller looks like:

@GetMapping
public Flux&lt;ResponseBody&gt; getAllResources(@RequestBody Resource resource) {
//related code here...
}

My Test method:

@Test
public void myTest() {

webClient.get()
.uri(&quot;uri_path&quot;)
.header(&quot;Content-Type&quot;, &quot;application/json&quot;)
.accept(&quot;application/json&quot;)
.exchange()
.expectedStatus.is2xxxSuccessful();
}

I was thinking since it is allowed in the controller to bind the object to POJO with the get call, there should be some way to test it using webTestClient.

答案1

得分: 5

尝试使用:
webTestClient.method(HttpMethod.GET)
代替:
webTestClient.get()

英文:

Try to use:

webTestClient.method(HttpMethod.GET)

instead of:

webTestClient.get()

huangapple
  • 本文由 发表于 2020年9月17日 06:29:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63928759.html
匿名

发表评论

匿名网友

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

确定