Commit 5320081d authored by Stephane Nicoll's avatar Stephane Nicoll

Revert "Only create a WebTestClient with WebFlux"

This reverts commit 282bd9f0
parent b80620fe
...@@ -6520,9 +6520,6 @@ example shows a class that uses both `@WebFluxTest` and a `WebTestClient`: ...@@ -6520,9 +6520,6 @@ example shows a class that uses both `@WebFluxTest` and a `WebTestClient`:
} }
---- ----
TIP: This setup is only supported by WebFlux applications as using `WebTestClient` in a
mocked web application only works with WebFlux at the moment.
A list of the auto-configuration that is enabled by `@WebFluxTest` can be A list of the auto-configuration that is enabled by `@WebFluxTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>. <<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
......
...@@ -29,8 +29,7 @@ import org.springframework.boot.test.autoconfigure.properties.PropertyMapping; ...@@ -29,8 +29,7 @@ import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
/** /**
* Annotation that can be applied to a test class to enable a {@link WebTestClient}. At * Annotation that can be applied to a test class to enable a {@link WebTestClient}.
* the moment, only WebFlux applications are supported.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 2.0.0 * @since 2.0.0
......
...@@ -22,11 +22,9 @@ import java.util.List; ...@@ -22,11 +22,9 @@ import java.util.List;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; 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.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration; 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.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.codec.CodecCustomizer; import org.springframework.boot.web.codec.CodecCustomizer;
...@@ -35,7 +33,6 @@ import org.springframework.context.annotation.Bean; ...@@ -35,7 +33,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.WebHandler;
/** /**
* Auto-configuration for {@link WebTestClient}. * Auto-configuration for {@link WebTestClient}.
...@@ -46,13 +43,12 @@ import org.springframework.web.server.WebHandler; ...@@ -46,13 +43,12 @@ import org.springframework.web.server.WebHandler;
*/ */
@Configuration @Configuration
@ConditionalOnClass({ WebClient.class, WebTestClient.class }) @ConditionalOnClass({ WebClient.class, WebTestClient.class })
@AutoConfigureAfter({ CodecsAutoConfiguration.class, WebFluxAutoConfiguration.class }) @AutoConfigureAfter(CodecsAutoConfiguration.class)
@EnableConfigurationProperties @EnableConfigurationProperties
public class WebTestClientAutoConfiguration { public class WebTestClientAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
@ConditionalOnBean(WebHandler.class)
public WebTestClient webTestClient(ApplicationContext applicationContext, public WebTestClient webTestClient(ApplicationContext applicationContext,
List<WebTestClientBuilderCustomizer> customizers) { List<WebTestClientBuilderCustomizer> customizers) {
WebTestClient.Builder builder = WebTestClient WebTestClient.Builder builder = WebTestClient
......
...@@ -18,15 +18,18 @@ package org.springframework.boot.test.autoconfigure.web.reactive; ...@@ -18,15 +18,18 @@ package org.springframework.boot.test.autoconfigure.web.reactive;
import java.time.Duration; import java.time.Duration;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.Collections;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.web.codec.CodecCustomizer; import org.springframework.boot.web.codec.CodecCustomizer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.http.codec.CodecConfigurer; import org.springframework.http.codec.CodecConfigurer;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
...@@ -41,43 +44,50 @@ import static org.mockito.Mockito.verify; ...@@ -41,43 +44,50 @@ import static org.mockito.Mockito.verify;
* Tests for {@link WebTestClientAutoConfiguration} * Tests for {@link WebTestClientAutoConfiguration}
* *
* @author Brian Clozel * @author Brian Clozel
* @author Stephane Nicoll
*/ */
public class WebTestClientAutoConfigurationTests { public class WebTestClientAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() private AnnotationConfigApplicationContext context;
.withConfiguration(AutoConfigurations.of(
WebTestClientAutoConfiguration.class));
@Test @After
public void shouldNotBeConfiguredWithoutWebHandler() { public void close() {
this.contextRunner.run((context) -> { if (this.context != null) {
assertThat(context).hasNotFailed(); this.context.close();
assertThat(context).doesNotHaveBean(WebTestClient.class); }
});
} }
@Test @Test
public void shouldCustomizeClientCodecs() { public void shouldCustomizeClientCodecs() {
this.contextRunner.withUserConfiguration(CodecConfiguration.class) load(CodecConfiguration.class);
.run((context) -> { WebTestClient webTestClient = this.context.getBean(WebTestClient.class);
assertThat(context).hasSingleBean(WebTestClient.class); CodecCustomizer codecCustomizer = this.context.getBean(CodecCustomizer.class);
assertThat(context).hasSingleBean(CodecCustomizer.class); assertThat(webTestClient).isNotNull();
verify(context.getBean(CodecCustomizer.class)).customize( verify(codecCustomizer).customize(any(CodecConfigurer.class));
any(CodecConfigurer.class));
});
} }
@Test @Test
public void shouldCustomizeTimeout() { public void shouldCustomizeTimeout() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class) PropertySource<?> propertySource = new MapPropertySource("test", Collections
.withPropertyValues("spring.test.webtestclient.timeout=15m") .singletonMap("spring.test.webtestclient.timeout", (Object) "PT15M"));
.run((context) -> { load(propertySource, BaseConfiguration.class);
WebTestClient webTestClient = context.getBean(WebTestClient.class); WebTestClient webTestClient = this.context.getBean(WebTestClient.class);
Object duration = ReflectionTestUtils.getField(webTestClient, Object duration = ReflectionTestUtils.getField(webTestClient, "timeout");
"timeout"); assertThat(duration).isEqualTo(Duration.of(15, ChronoUnit.MINUTES));
assertThat(duration).isEqualTo(Duration.of(15, ChronoUnit.MINUTES)); }
});
private void load(Class<?>... config) {
load(null, config);
}
private void load(PropertySource<?> propertySource, Class<?>... config) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
if (propertySource != null) {
context.getEnvironment().getPropertySources().addFirst(propertySource);
}
context.register(config);
context.register(WebTestClientAutoConfiguration.class);
context.refresh();
this.context = context;
} }
@Configuration @Configuration
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment