WebTestClient with get() and requestBody not available

1.6k views Asked by At

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

There are 1 answers

0
Omi_n On

Try to use:

webTestClient.method(HttpMethod.GET)

instead of:

webTestClient.get()