With Spring Boot 2.4.2 I'm using the WebTestClient to invoke requests in my integration tests.
This is the first request which gets a list of messages:
webTestClient
.get()
.uri("/api/messages")
.headers(http -> http.setBearerAuth(token))
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(APPLICATION_JSON)
.expectBody()
.jsonPath("$.length()").isEqualTo(1)
.jsonPath("$[0].id").isNumber()
.jsonPath("$[0].type").isEqualTo(4);
Now I'd like to invoke a subsequent request to download a specific message. For this, I need the id which was already checked with jsonPath("$[0].id").
webTestClient
.get()
.uri(uriBuilder -> uriBuilder.path("/api/messages/{id}").build(extractedId))
.headers(http -> http.setBearerAuth(token))
.exchange()
.expectStatus().isOk();
How can I extract this id into a local variable or else, so that it's available for the second request?
You can check their official docs for it.
But expanding the answer a bit, easiest way would be this
There are also ways to get an (infinite) stream of responses explained on docs, which is pretty similar to the example above.