英文:
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<ResponseBody> getAllResources(@RequestBody Resource resource) {
//related code here...
}
My Test method:
@Test
public void myTest() {
webClient.get()
.uri("uri_path")
.header("Content-Type", "application/json")
.accept("application/json")
.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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论