Assert WebFlux present for WebTestClient

Closes gh-26308
This commit is contained in:
Rossen Stoyanchev 2021-01-06 21:20:23 +00:00
parent c040cd7b05
commit fb2e53276c
1 changed files with 9 additions and 1 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -57,6 +57,8 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {
private static final boolean httpComponentsClientPresent;
private static final boolean webFluxPresent;
static {
ClassLoader loader = DefaultWebTestClientBuilder.class.getClassLoader();
reactorClientPresent = ClassUtils.isPresent("reactor.netty.http.client.HttpClient", loader);
@ -64,6 +66,8 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {
httpComponentsClientPresent =
ClassUtils.isPresent("org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient", loader) &&
ClassUtils.isPresent("org.apache.hc.core5.reactive.ReactiveDataConsumer", loader);
webFluxPresent = ClassUtils.isPresent(
"org.springframework.web.reactive.function.client.ExchangeFunction", loader);
}
@ -119,6 +123,10 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {
Assert.isTrue(httpHandlerBuilder == null || connector == null,
"Expected WebHttpHandlerBuilder or ClientHttpConnector but not both.");
// Helpful message especially for MockMvcWebTestClient users
Assert.state(webFluxPresent,
"To use WebTestClient, please add spring-webflux to the test classpath.");
this.connector = connector;
this.httpHandlerBuilder = (httpHandlerBuilder != null ? httpHandlerBuilder.clone() : null);
}