簡體   English   中英

如何從返回 Mono 的反應端點測試 Reactor WebClient 主體響應<responseentity<flux<mydto> >>? </responseentity<flux<mydto>

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

我在我的 Spring 引導項目中使用了來自 org.openapitools 的開放式 openapi-generator-maven-plugin 並啟用了反應配置。 我的端點之一返回一個列表正文響應,該響應自動生成為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>

如何使用WebTestClient在集成測試中測試端點 Controller 的主體?

如果我嘗試這樣做,它不會起作用,因為我收到的是通量,而不是預期的 dto object。

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

我終於自己解決了。

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

我將 ResponseBody 嵌套到 StepVerifier 中,就像我們通常測試標准 Producer (Flux) 時所做的那樣

如何轉換 Reactor Flux<object> 至 Mono<void><div id="text_translate"><p> 我定義了一個數據提供程序幫助 class 以在測試期間填充我的本地數據庫,並且我使用的是 ReactorCrudRepository。 Te 定義的 saveAll 方法是:</p><pre> <S extends T> Flux<S> saveAll(Publisher<S> entityStream);</pre><p> 我的 PostgresDataProvider.insertData 返回 Mono 因為我對接收插入的實體不感興趣。 如何將我當前的方法從 Flux 傳輸到 Mono?</p><pre> public Mono<Void> insertData(Flux<Entity> entities) { return repository.saveAll(entities); }</pre></div></void></object>

[英]How to convert Reactor Flux<Object> to Mono<Void>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM