![](/img/trans.png)
[英]How to test Controller that returns Mono<ResponseEntity<Void>>?
[英]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) 時所做的那樣
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.