Only create a WebTestClient with WebFlux

This commit updates WebTestClientAutoConfiguration to only create a
WebTestClient when running a WebFlux-based application as mocking the
context only works with that mode at the moment.

Closes gh-12318
This commit is contained in:
Stephane Nicoll
2018-03-14 05:12:32 -04:00
parent 26d9c261c5
commit 282bd9f0db
4 changed files with 37 additions and 39 deletions

View File

@@ -29,7 +29,8 @@ import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
import org.springframework.test.web.reactive.server.WebTestClient;
/**
* Annotation that can be applied to a test class to enable a {@link WebTestClient}.
* Annotation that can be applied to a test class to enable a {@link WebTestClient}. At
* the moment, only WebFlux applications are supported.
*
* @author Stephane Nicoll
* @since 2.0.0

View File

@@ -22,9 +22,11 @@ import java.util.List;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.codec.CodecCustomizer;
@@ -33,6 +35,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.WebHandler;
/**
* Auto-configuration for {@link WebTestClient}.
@@ -43,12 +46,13 @@ import org.springframework.web.reactive.function.client.WebClient;
*/
@Configuration
@ConditionalOnClass({ WebClient.class, WebTestClient.class })
@AutoConfigureAfter(CodecsAutoConfiguration.class)
@AutoConfigureAfter({ CodecsAutoConfiguration.class, WebFluxAutoConfiguration.class })
@EnableConfigurationProperties
public class WebTestClientAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(WebHandler.class)
public WebTestClient webTestClient(ApplicationContext applicationContext,
List<WebTestClientBuilderCustomizer> customizers) {
WebTestClient.Builder builder = WebTestClient