How to test Reactor WebClient body response from a reactive endpoint that returns Mono<ResponseEntity<Flux<MyDto>>>?

932 Views Asked by At

I used open openapi-generator-maven-plugin from org.openapitools in my Spring Boot project with the reactive configuration enabled. One of my endpoint returns a List body response that is auto-generated as Mono<ResponseEntity<Flux>>

    <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>${openapi-generator.version}</version>
        <configuration>
          ...
          <configOptions>
            <interfaceOnly>true</interfaceOnly>
            <reactive>true</reactive>
            ...
          </configOptions>
        </configuration>    
    </plugin>

How can I test the body of my endpoint Controller within an integration test using WebTestClient?

If I try this it doesn't work due to I'm receiving a flux instead of the expected dto object.

webTestApi.get()
    .uri("my_uri")
    .accept(MediaType.APPLICATION_JSON)
    .exchange()
    .expectStatus().isOk()
    .expectHeader().contentType(MediaType.APPLICATION_JSON)
    .expectBody(MyDto.class)
    .isEqualTo(myDto);
1

There are 1 best solutions below

0
On

I finally resolved it by my own.

StepVerifier.create(rateApi.get()
            .uri("...")
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().isOk()
            .returnResult(MyDto.class).getResponseBody())
            .expectNext(myDto)
            .verifyComplete();

I nested the ResponseBody into a StepVerifier as we usually do for testing a standard Producer (Flux)