This commit is contained in:
Stéphane Nicoll 2025-10-07 10:39:54 +02:00
parent 5ec993bb8c
commit 533544d8fc
6 changed files with 21 additions and 44 deletions

View File

@ -18,13 +18,12 @@ package org.springframework.boot.test.web.reactive.client;
import org.springframework.context.ApplicationContext;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.reactive.server.WebTestClient.Builder;
/**
* A customizer that can be implemented by beans wishing to customize the {@link Builder}
* to fine-tune its auto-configuration before a {@link WebTestClient} is created.
* Implementations can be registered in the {@link ApplicationContext} or
* {@code spring.factories}.
* A customizer that can be implemented by beans wishing to customize the
* {@link WebTestClient.Builder} to fine-tune its auto-configuration before a
* {@link WebTestClient} is created. Implementations can be registered in the
* {@link ApplicationContext} or {@code spring.factories}.
*
* @author Andy Wilkinson
* @since 4.0.0
@ -36,6 +35,6 @@ public interface WebTestClientBuilderCustomizer {
* Customize the given {@code builder}.
* @param builder the builder
*/
void customize(Builder builder);
void customize(WebTestClient.Builder builder);
}

View File

@ -123,10 +123,6 @@ class WebTestClientContextCustomizer implements ContextCustomizer {
}
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}
/**
@ -143,11 +139,6 @@ class WebTestClientContextCustomizer implements ContextCustomizer {
this.applicationContext = applicationContext;
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public Class<?> getObjectType() {
return WebTestClient.class;
@ -164,7 +155,7 @@ class WebTestClientContextCustomizer implements ContextCustomizer {
private WebTestClient createWebTestClient() {
Assert.state(this.applicationContext != null, "ApplicationContext not injected");
WebTestClient.Builder builder = WebTestClient.bindToServer();
customizeWebTestClientBuilder(builder, this.applicationContext);
customizeWebTestClientBuilder(builder);
BaseUrl baseUrl = new BaseUrlProviders(this.applicationContext).getBaseUrl();
if (baseUrl != null) {
builder.baseUrl(baseUrl.resolve());
@ -172,7 +163,7 @@ class WebTestClientContextCustomizer implements ContextCustomizer {
return builder.build();
}
private void customizeWebTestClientBuilder(WebTestClient.Builder clientBuilder, ApplicationContext context) {
private void customizeWebTestClientBuilder(WebTestClient.Builder clientBuilder) {
Assert.state(this.applicationContext != null, "ApplicationContext not injected");
getWebTestClientBuilderCustomizers(this.applicationContext)
.forEach((customizer) -> customizer.customize(clientBuilder));
@ -180,10 +171,9 @@ class WebTestClientContextCustomizer implements ContextCustomizer {
private List<WebTestClientBuilderCustomizer> getWebTestClientBuilderCustomizers(ApplicationContext context) {
List<WebTestClientBuilderCustomizer> customizers = new ArrayList<>();
SpringFactoriesLoader.forDefaultResourceLocation(context.getClassLoader())
.load(WebTestClientBuilderCustomizer.class, ArgumentResolver.of(ApplicationContext.class, context))
.forEach(customizers::add);
context.getBeansOfType(WebTestClientBuilderCustomizer.class).values().forEach(customizers::add);
customizers.addAll(SpringFactoriesLoader.forDefaultResourceLocation(context.getClassLoader())
.load(WebTestClientBuilderCustomizer.class, ArgumentResolver.of(ApplicationContext.class, context)));
customizers.addAll(context.getBeansOfType(WebTestClientBuilderCustomizer.class).values());
return customizers;
}

View File

@ -18,13 +18,12 @@ package org.springframework.boot.test.web.servlet.client;
import org.springframework.context.ApplicationContext;
import org.springframework.test.web.servlet.client.RestTestClient;
import org.springframework.test.web.servlet.client.RestTestClient.Builder;
/**
* A customizer that can be implemented by beans wishing to customize the {@link Builder}
* to fine-tune its auto-configuration before a {@link RestTestClient} is created.
* Implementations can be registered in the {@link ApplicationContext} or
* {@code spring.factories}.
* A customizer that can be implemented by beans wishing to customize the
* {@link RestTestClient.Builder} to fine-tune its auto-configuration before a
* {@link RestTestClient} is created. Implementations can be registered in the
* {@link ApplicationContext} or {@code spring.factories}.
*
* @author Stephane Nicoll
* @since 4.0.0
@ -33,9 +32,9 @@ import org.springframework.test.web.servlet.client.RestTestClient.Builder;
public interface RestTestClientBuilderCustomizer {
/**
* Customize the given {@link Builder Builder}.
* Customize the given {@link RestTestClient.Builder Builder}.
* @param builder the builder
*/
void customize(Builder<?> builder);
void customize(RestTestClient.Builder<?> builder);
}

View File

@ -125,10 +125,6 @@ class RestTestClientContextCustomizer implements ContextCustomizer {
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}
/**
@ -145,11 +141,6 @@ class RestTestClientContextCustomizer implements ContextCustomizer {
this.applicationContext = applicationContext;
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public Class<?> getObjectType() {
return RestTestClient.class;
@ -166,13 +157,12 @@ class RestTestClientContextCustomizer implements ContextCustomizer {
private RestTestClient createRestTestClient() {
Assert.state(this.applicationContext != null, "ApplicationContext not injected");
RestTestClient.Builder<?> builder = RestTestClient.bindToServer();
customizeRestTestClientBuilder(builder, this.applicationContext);
customizeRestTestClientBuilder(builder);
BaseUrl baseUrl = new BaseUrlProviders(this.applicationContext).getBaseUrl();
return builder.uriBuilderFactory(BaseUrlUriBuilderFactory.get(baseUrl)).build();
}
private void customizeRestTestClientBuilder(RestTestClient.Builder<?> clientBuilder,
ApplicationContext context) {
private void customizeRestTestClientBuilder(RestTestClient.Builder<?> clientBuilder) {
Assert.state(this.applicationContext != null, "ApplicationContext not injected");
getRestTestClientBuilderCustomizers(this.applicationContext)
.forEach((customizer) -> customizer.customize(clientBuilder));
@ -180,10 +170,9 @@ class RestTestClientContextCustomizer implements ContextCustomizer {
private List<RestTestClientBuilderCustomizer> getRestTestClientBuilderCustomizers(ApplicationContext context) {
List<RestTestClientBuilderCustomizer> customizers = new ArrayList<>();
SpringFactoriesLoader.forDefaultResourceLocation(context.getClassLoader())
.load(RestTestClientBuilderCustomizer.class, ArgumentResolver.of(ApplicationContext.class, context))
.forEach(customizers::add);
context.getBeansOfType(RestTestClientBuilderCustomizer.class).values().forEach(customizers::add);
customizers.addAll(SpringFactoriesLoader.forDefaultResourceLocation(context.getClassLoader())
.load(RestTestClientBuilderCustomizer.class, ArgumentResolver.of(ApplicationContext.class, context)));
customizers.addAll(context.getBeansOfType(RestTestClientBuilderCustomizer.class).values());
return customizers;
}