Skip to content

Commit 96f85a4

Browse files
committed
Support customization of WebTestClient.Builder when using @SpringBootTest
Closes gh-15132
1 parent 100fc8c commit 96f85a4

File tree

7 files changed

+74
-7
lines changed

7 files changed

+74
-7
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6052,6 +6052,13 @@ include::{code-examples}/test/web/RandomPortTestRestTemplateExampleTests.java[ta
60526052

60536053

60546054

6055+
[[boot-features-testing-spring-boot-applications-customizing-web-test-client]]
6056+
==== Customizing WebTestClient
6057+
To customize the `WebTestClient` bean, configure a `WebTestClientBuilderCustomizer` bean.
6058+
Any such beans are called with the `WebTestClient.Builder` that is used to create the `WebTestClient`.
6059+
6060+
6061+
60556062
[[boot-features-testing-spring-boot-applications-jmx]]
60566063
==== Using JMX
60576064
As the test context framework caches context, JMX is disabled by default to prevent identical components to register on the same domain.

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/reactive/SpringBootWebTestClientBuilderCustomizer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collection;
2121
import java.util.function.Consumer;
2222

23+
import org.springframework.boot.test.web.reactive.server.WebTestClientBuilderCustomizer;
2324
import org.springframework.boot.web.codec.CodecCustomizer;
2425
import org.springframework.http.codec.ClientCodecConfigurer;
2526
import org.springframework.test.web.reactive.server.WebTestClient;

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/reactive/WebTestClientAutoConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
2929
import org.springframework.boot.context.properties.ConfigurationProperties;
3030
import org.springframework.boot.context.properties.EnableConfigurationProperties;
31+
import org.springframework.boot.test.web.reactive.server.WebTestClientBuilderCustomizer;
3132
import org.springframework.boot.web.codec.CodecCustomizer;
3233
import org.springframework.context.ApplicationContext;
3334
import org.springframework.context.annotation.Bean;

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/reactive/WebTestClientBuilderCustomizer.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@
2626
* @author Andy Wilkinson
2727
* @since 2.0.0
2828
* @see WebTestClientAutoConfiguration
29+
* @deprecated since 2.2 in favor of
30+
* {@link org.springframework.boot.test.web.reactive.server.WebTestClientBuilderCustomizer}
2931
*/
3032
@FunctionalInterface
31-
public interface WebTestClientBuilderCustomizer {
32-
33-
/**
34-
* Customize the given {@code builder}.
35-
* @param builder the builder
36-
*/
37-
void customize(Builder builder);
33+
@Deprecated
34+
public interface WebTestClientBuilderCustomizer
35+
extends org.springframework.boot.test.web.reactive.server.WebTestClientBuilderCustomizer {
3836

3937
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.web.reactive.server;
18+
19+
import org.springframework.test.web.reactive.server.WebTestClient.Builder;
20+
21+
/**
22+
* A customizer for a {@link Builder}. Any {@code WebTestClientBuilderCustomizer} beans
23+
* found in the application context will be {@link #customize called} to customize the
24+
* auto-configured {@link Builder}.
25+
*
26+
* @author Andy Wilkinson
27+
* @since 2.2.0
28+
*/
29+
@FunctionalInterface
30+
public interface WebTestClientBuilderCustomizer {
31+
32+
/**
33+
* Customize the given {@code builder}.
34+
* @param builder the builder
35+
*/
36+
void customize(Builder builder);
37+
38+
}

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/reactive/server/WebTestClientContextCustomizer.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ private WebTestClient createWebTestClient() {
160160
String port = this.applicationContext.getEnvironment().getProperty("local.server.port", "8080");
161161
String baseUrl = (sslEnabled ? "https" : "http") + "://localhost:" + port;
162162
WebTestClient.Builder builder = WebTestClient.bindToServer();
163+
customizeWebTestClientBuilder(builder, this.applicationContext);
163164
customizeWebTestClientCodecs(builder, this.applicationContext);
164165
return builder.baseUrl(baseUrl).build();
165166
}
@@ -175,6 +176,13 @@ private boolean isSslEnabled(ApplicationContext context) {
175176
}
176177
}
177178

179+
private void customizeWebTestClientBuilder(WebTestClient.Builder clientBuilder, ApplicationContext context) {
180+
for (WebTestClientBuilderCustomizer customizer : context
181+
.getBeansOfType(WebTestClientBuilderCustomizer.class).values()) {
182+
customizer.customize(clientBuilder);
183+
}
184+
}
185+
178186
private void customizeWebTestClientCodecs(WebTestClient.Builder clientBuilder, ApplicationContext context) {
179187
Collection<CodecCustomizer> codecCustomizers = context.getBeansOfType(CodecCustomizer.class).values();
180188
if (!CollectionUtils.isEmpty(codecCustomizers)) {

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/reactive/server/WebTestClientContextCustomizerIntegrationTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
import org.springframework.http.server.reactive.ServerHttpResponse;
3434
import org.springframework.test.annotation.DirtiesContext;
3535
import org.springframework.test.web.reactive.server.WebTestClient;
36+
import org.springframework.test.web.reactive.server.WebTestClient.Builder;
37+
38+
import static org.mockito.ArgumentMatchers.any;
39+
import static org.mockito.Mockito.mock;
40+
import static org.mockito.Mockito.verify;
3641

3742
/**
3843
* Integration test for {@link WebTestClientContextCustomizer}.
@@ -46,8 +51,12 @@ class WebTestClientContextCustomizerIntegrationTests {
4651
@Autowired
4752
private WebTestClient webTestClient;
4853

54+
@Autowired
55+
private WebTestClientBuilderCustomizer clientBuilderCustomizer;
56+
4957
@Test
5058
void test() {
59+
verify(this.clientBuilderCustomizer).customize(any(Builder.class));
5160
this.webTestClient.get().uri("/").exchange().expectBody(String.class).isEqualTo("hello");
5261
}
5362

@@ -60,6 +69,11 @@ TomcatReactiveWebServerFactory webServerFactory() {
6069
return new TomcatReactiveWebServerFactory(0);
6170
}
6271

72+
@Bean
73+
WebTestClientBuilderCustomizer clientBuilderCustomizer() {
74+
return mock(WebTestClientBuilderCustomizer.class);
75+
}
76+
6377
}
6478

6579
static class TestHandler implements HttpHandler {

0 commit comments

Comments
 (0)