【发布时间】:2021-12-19 17:58:24
【问题描述】:
我正在尝试模拟 Spring WebClient 并且遇到了 WebClient.builder() 模拟的问题。到目前为止我在测试中定义的模拟没有被使用,我认为是因为这个构建器没有返回我的模拟 WebClient。如何让 builder 退回我的模型?
WebClient webClient = WebClient.builder()
.exchangeStrategies(ExchangeStrategies.builder()
.codecs(configurer -> configurer
.defaultCodecs()
.maxInMemorySize(16 * 1024 * 1024))
.build())
.clientConnector(new ReactorClientHttpConnector(HttpClient.newConnection().compress(true)))
.build();
WebClient 就是这样使用的:
response = webClient
.get()
.uri(URLDecoder.decode(someEndpoint.replace("\"", ""), CLIENT_ENCODING))
.header(AUTHORIZATION, BEARER + accessToken)
.accept(MediaType.APPLICATION_JSON)
.retrieve().bodyToMono(JsonNode.class).block();
当我遵循以下解决方案时,我没有看到模拟有任何问题:How to mock Spring WebFlux WebClient? 但没有使用模拟。如何模拟构建器/从构建器返回我的模拟?
【问题讨论】:
标签: java spring rest unit-testing mockito