diff --git a/core/spring-boot-test/src/main/java/org/springframework/boot/test/http/server/LazyUriBuilderFactory.java b/core/spring-boot-test/src/main/java/org/springframework/boot/test/http/server/LazyUriBuilderFactory.java index 146e0740136..2769277a90e 100644 --- a/core/spring-boot-test/src/main/java/org/springframework/boot/test/http/server/LazyUriBuilderFactory.java +++ b/core/spring-boot-test/src/main/java/org/springframework/boot/test/http/server/LazyUriBuilderFactory.java @@ -30,13 +30,14 @@ import org.springframework.web.util.UriBuilderFactory; * Lazy {@link UriBuilderFactory} that only obtains the delegate on first call. * * @author Phillip Webb + * @author Stephane Nicoll */ class LazyUriBuilderFactory implements UriBuilderFactory { - private final Supplier suppler; + private final Supplier supplier; LazyUriBuilderFactory(Supplier supplier) { - this.suppler = SingletonSupplier.of(supplier); + this.supplier = SingletonSupplier.of(supplier); } @Override @@ -60,7 +61,7 @@ class LazyUriBuilderFactory implements UriBuilderFactory { } private UriBuilderFactory delegate() { - return this.suppler.get(); + return this.supplier.get(); } } diff --git a/core/spring-boot-test/src/main/java/org/springframework/boot/test/http/server/LocalTestWebServer.java b/core/spring-boot-test/src/main/java/org/springframework/boot/test/http/server/LocalTestWebServer.java index 572db5884ef..bbe445a66bd 100644 --- a/core/spring-boot-test/src/main/java/org/springframework/boot/test/http/server/LocalTestWebServer.java +++ b/core/spring-boot-test/src/main/java/org/springframework/boot/test/http/server/LocalTestWebServer.java @@ -43,18 +43,22 @@ public final class LocalTestWebServer { private final Scheme scheme; - private final SingletonSupplier connection; + private final SingletonSupplier baseUriDetails; - private LocalTestWebServer(Scheme scheme, Supplier connectionSupplier) { + private final UriBuilderFactory uriBuilderFactory; + + private LocalTestWebServer(Scheme scheme, Supplier baseUriDetailsSupplier) { Assert.notNull(scheme, "'scheme' must not be null"); - Assert.notNull(connectionSupplier, "'connectionSupplier' must not be null"); + Assert.notNull(baseUriDetailsSupplier, "'baseUriDetailsSupplier' must not be null"); this.scheme = scheme; - this.connection = SingletonSupplier.of(connectionSupplier); + this.baseUriDetails = SingletonSupplier.of(baseUriDetailsSupplier); + this.uriBuilderFactory = new LazyUriBuilderFactory( + () -> new DefaultUriBuilderFactory(this.baseUriDetails.obtain().uri(scheme()))); } /** - * Return if URI scheme used for the connection. This method can be safely called - * before the local test server is fully running. + * Return if URI scheme used for the request. This method can be safely called before + * the local test server is fully running. * @return if the web server uses an HTTPS address */ public Scheme scheme() { @@ -100,7 +104,7 @@ public final class LocalTestWebServer { * @return a new {@link UriBuilderFactory} */ public UriBuilderFactory uriBuilderFactory() { - return new LazyUriBuilderFactory(() -> new DefaultUriBuilderFactory(getConnection().uri(scheme()))); + return this.uriBuilderFactory; } /** @@ -110,13 +114,7 @@ public final class LocalTestWebServer { * @return a new instance with the path added */ public LocalTestWebServer withPath(String path) { - return of(this.scheme, () -> getConnection().withPath(path)); - } - - private Connection getConnection() { - Connection connection = this.connection.get(); - Assert.state(connection != null, "No local test web server connection supplied"); - return connection; + return of(this.scheme, () -> this.baseUriDetails.obtain().withPath(path)); } /** @@ -137,33 +135,33 @@ public final class LocalTestWebServer { * @return a new {@link LocalTestWebServer} instance */ public static LocalTestWebServer of(Scheme scheme, int port, @Nullable String contextPath) { - return of(scheme, () -> new Connection(port, (contextPath != null) ? contextPath : "")); + return of(scheme, () -> new BaseUriDetails(port, (contextPath != null) ? contextPath : "")); } /** * Factory method to create a new {@link LocalTestWebServer} instance. * @param scheme the URL scheme - * @param connectionSupplier a supplier to provide the server connection + * @param baseUriDetailsSupplier a supplier to provide the details of the base URI * @return a new {@link LocalTestWebServer} instance */ - public static LocalTestWebServer of(Scheme scheme, Supplier connectionSupplier) { - return new LocalTestWebServer(scheme, connectionSupplier); + public static LocalTestWebServer of(Scheme scheme, Supplier baseUriDetailsSupplier) { + return new LocalTestWebServer(scheme, baseUriDetailsSupplier); } /** - * Return a {@link LocalTestWebServer} instance provided from the + * Obtain the {@link LocalTestWebServer} instance provided from the * {@link ApplicationContext}. * @param applicationContext the application context - * @return the local test web server or {@code null} + * @return the local test web server (never {@code null}) */ - public static LocalTestWebServer getRequired(ApplicationContext applicationContext) { + public static LocalTestWebServer obtain(ApplicationContext applicationContext) { LocalTestWebServer localTestWebServer = get(applicationContext); Assert.state(localTestWebServer != null, "No local test web server available"); return localTestWebServer; } /** - * Return a {@link LocalTestWebServer} instance provided from the + * Return the {@link LocalTestWebServer} instance provided from the * {@link ApplicationContext} or {@code null} of no local server is started or could * be provided. * @param applicationContext the application context @@ -182,23 +180,19 @@ public final class LocalTestWebServer { } /** - * Details of a connection to the local test web server. + * Details of the base URI to the local test web server. * * @param port the port of the running server - * @param path the path of the running server + * @param path the path to use */ - public record Connection(int port, String path) { + public record BaseUriDetails(int port, String path) { String uri(Scheme scheme) { - StringBuilder uri = new StringBuilder(scheme.name().toLowerCase(Locale.getDefault())); - uri.append("://localhost:"); - uri.append(port()); - uri.append(path()); - return uri.toString(); + return scheme.name().toLowerCase(Locale.ROOT) + "://localhost:" + port() + path(); } - Connection withPath(String path) { - return new Connection(port(), path() + path); + BaseUriDetails withPath(String path) { + return new BaseUriDetails(port(), path() + path); } } @@ -221,10 +215,9 @@ public final class LocalTestWebServer { } /** - * Strategy used to provide the running {@link LocalTestWebServer}. Implementations - * can be registered in {@code spring.factories} and may accept an + * Internal strategy used to provide the running {@link LocalTestWebServer}. + * Implementations can be registered in {@code spring.factories} and may accept an * {@link ApplicationContext} constructor argument. - * */ @FunctionalInterface public interface Provider { diff --git a/core/spring-boot-test/src/main/java/org/springframework/boot/test/web/htmlunit/UriBuilderFactoryWebClient.java b/core/spring-boot-test/src/main/java/org/springframework/boot/test/web/htmlunit/UriBuilderFactoryWebClient.java index 348c3bbf3f9..6f46a90e0eb 100644 --- a/core/spring-boot-test/src/main/java/org/springframework/boot/test/web/htmlunit/UriBuilderFactoryWebClient.java +++ b/core/spring-boot-test/src/main/java/org/springframework/boot/test/web/htmlunit/UriBuilderFactoryWebClient.java @@ -34,7 +34,7 @@ import org.springframework.web.util.UriBuilderFactory; */ public class UriBuilderFactoryWebClient extends WebClient { - private UriBuilderFactory uriBuilderFactory; + private final UriBuilderFactory uriBuilderFactory; public UriBuilderFactoryWebClient(UriBuilderFactory uriBuilderFactory) { Assert.notNull(uriBuilderFactory, "'uriBuilderFactory' must not be null"); @@ -43,8 +43,7 @@ public class UriBuilderFactoryWebClient extends WebClient { @Override public

P getPage(String url) throws IOException, FailingHttpStatusCodeException { - return super.getPage( - (this.uriBuilderFactory != null) ? this.uriBuilderFactory.uriString(url).toUriString() : url); + return super.getPage(this.uriBuilderFactory.uriString(url).toUriString()); } } diff --git a/core/spring-boot-test/src/main/java/org/springframework/boot/test/web/htmlunit/UriBuilderFactoryWebConnectionHtmlUnitDriver.java b/core/spring-boot-test/src/main/java/org/springframework/boot/test/web/htmlunit/UriBuilderFactoryWebConnectionHtmlUnitDriver.java index b7674b974dc..5dde797dd98 100644 --- a/core/spring-boot-test/src/main/java/org/springframework/boot/test/web/htmlunit/UriBuilderFactoryWebConnectionHtmlUnitDriver.java +++ b/core/spring-boot-test/src/main/java/org/springframework/boot/test/web/htmlunit/UriBuilderFactoryWebConnectionHtmlUnitDriver.java @@ -32,7 +32,7 @@ import org.springframework.web.util.UriBuilderFactory; */ public class UriBuilderFactoryWebConnectionHtmlUnitDriver extends WebConnectionHtmlUnitDriver { - private UriBuilderFactory uriBuilderFactory; + private final UriBuilderFactory uriBuilderFactory; public UriBuilderFactoryWebConnectionHtmlUnitDriver(UriBuilderFactory uriBuilderFactory) { Assert.notNull(uriBuilderFactory, "'uriBuilderFactory' must not be null"); @@ -60,6 +60,7 @@ public class UriBuilderFactoryWebConnectionHtmlUnitDriver extends WebConnectionH } @Override + @SuppressWarnings("ConstantValue") // default constructor calls this method public void get(String url) { super.get((this.uriBuilderFactory != null) ? this.uriBuilderFactory.uriString(url).toUriString() : url); } diff --git a/core/spring-boot-test/src/test/java/org/springframework/boot/test/http/server/LocalTestWebServerTests.java b/core/spring-boot-test/src/test/java/org/springframework/boot/test/http/server/LocalTestWebServerTests.java index b86a7e0b39d..55e618a6f2b 100644 --- a/core/spring-boot-test/src/test/java/org/springframework/boot/test/http/server/LocalTestWebServerTests.java +++ b/core/spring-boot-test/src/test/java/org/springframework/boot/test/http/server/LocalTestWebServerTests.java @@ -23,7 +23,7 @@ import java.util.concurrent.atomic.AtomicInteger; import org.jspecify.annotations.Nullable; import org.junit.jupiter.api.Test; -import org.springframework.boot.test.http.server.LocalTestWebServer.Connection; +import org.springframework.boot.test.http.server.LocalTestWebServer.BaseUriDetails; import org.springframework.boot.test.http.server.LocalTestWebServer.Scheme; import org.springframework.boot.testsupport.classpath.resources.WithResource; import org.springframework.context.ApplicationContext; @@ -129,14 +129,21 @@ class LocalTestWebServerTests { } @Test - void uriUsesSingletonConnection() { + void uriUsesSingletonBaseUriDetails() { AtomicInteger counter = new AtomicInteger(); LocalTestWebServer server = LocalTestWebServer.of(Scheme.HTTPS, - () -> new Connection(8080, "/" + counter.incrementAndGet())); + () -> new BaseUriDetails(8080, "/" + counter.incrementAndGet())); assertThat(server.uri()).isEqualTo("https://localhost:8080/1"); assertThat(server.uri()).isEqualTo("https://localhost:8080/1"); } + @Test + void uriBuilderFactoryUsesSingletonUriBuilderFactory() { + LocalTestWebServer server = LocalTestWebServer.of(Scheme.HTTPS, () -> new BaseUriDetails(8080, "/")); + UriBuilderFactory uriBuilderFactory = server.uriBuilderFactory(); + assertThat(server.uriBuilderFactory()).isSameAs(uriBuilderFactory); + } + @Test void withPathCreatedNewInstance() { assertThat(LocalTestWebServer.of(Scheme.HTTPS, 8080, "/path").withPath("/other").uri()) @@ -145,9 +152,9 @@ class LocalTestWebServerTests { @Test @SuppressWarnings("NullAway") // Test null check - void ofWhenConnectionSupplierIsNull() { + void ofWhenBaseUriDetailsSupplierIsNull() { assertThatIllegalArgumentException().isThrownBy(() -> LocalTestWebServer.of(Scheme.HTTPS, null)) - .withMessage("'connectionSupplier' must not be null"); + .withMessage("'baseUriDetailsSupplier' must not be null"); } @Test @@ -180,12 +187,13 @@ class LocalTestWebServerTests { org.springframework.boot.test.http.server.LocalTestWebServer$Provider=\ org.springframework.boot.test.http.server.LocalTestWebServerTests$Provider1 """) - void getRequiredWhenNoneProvidedThrowsException() { + void obtainWhenNoneProvidedThrowsException() { ApplicationContext applicationContext = new GenericApplicationContext(); - assertThatIllegalStateException().isThrownBy(() -> LocalTestWebServer.getRequired(applicationContext)) + assertThatIllegalStateException().isThrownBy(() -> LocalTestWebServer.obtain(applicationContext)) .withMessage("No local test web server available"); } + @SuppressWarnings("unused") static class Provider1 implements LocalTestWebServer.Provider { @Override @@ -195,6 +203,7 @@ class LocalTestWebServerTests { } + @SuppressWarnings("unused") static class Provider2 implements LocalTestWebServer.Provider { @Override @@ -204,6 +213,7 @@ class LocalTestWebServerTests { } + @SuppressWarnings("unused") static class Provider3 implements LocalTestWebServer.Provider { @Override diff --git a/module/spring-boot-graphql-test/src/test/java/org/springframework/boot/graphql/test/autoconfigure/tester/HttpGraphQlTesterAutoConfigurationTests.java b/module/spring-boot-graphql-test/src/test/java/org/springframework/boot/graphql/test/autoconfigure/tester/HttpGraphQlTesterAutoConfigurationTests.java index af7b24a1631..7b005cb4880 100644 --- a/module/spring-boot-graphql-test/src/test/java/org/springframework/boot/graphql/test/autoconfigure/tester/HttpGraphQlTesterAutoConfigurationTests.java +++ b/module/spring-boot-graphql-test/src/test/java/org/springframework/boot/graphql/test/autoconfigure/tester/HttpGraphQlTesterAutoConfigurationTests.java @@ -102,6 +102,7 @@ class HttpGraphQlTesterAutoConfigurationTests { }); } + @SuppressWarnings("unused") static class TestLocalTestWebServerProvider implements LocalTestWebServer.Provider { @Override diff --git a/module/spring-boot-resttestclient/src/main/java/org/springframework/boot/resttestclient/autoconfigure/TestRestTemplateAutoConfiguration.java b/module/spring-boot-resttestclient/src/main/java/org/springframework/boot/resttestclient/autoconfigure/TestRestTemplateAutoConfiguration.java index d63138dde90..6245b33f8f2 100644 --- a/module/spring-boot-resttestclient/src/main/java/org/springframework/boot/resttestclient/autoconfigure/TestRestTemplateAutoConfiguration.java +++ b/module/spring-boot-resttestclient/src/main/java/org/springframework/boot/resttestclient/autoconfigure/TestRestTemplateAutoConfiguration.java @@ -41,7 +41,7 @@ public final class TestRestTemplateAutoConfiguration { TestRestTemplate testRestTemplate(ObjectProvider builderProvider, ApplicationContext applicationContext) { RestTemplateBuilder builder = builderProvider.getIfAvailable(RestTemplateBuilder::new); - LocalTestWebServer localTestWebServer = LocalTestWebServer.getRequired(applicationContext); + LocalTestWebServer localTestWebServer = LocalTestWebServer.obtain(applicationContext); TestRestTemplate template = new TestRestTemplate(builder, null, null, httpClientOptions(localTestWebServer.scheme())); template.setUriTemplateHandler(localTestWebServer.uriBuilderFactory()); diff --git a/module/spring-boot-resttestclient/src/test/java/org/springframework/boot/resttestclient/autoconfigure/RestTestClientAutoConfigurationTests.java b/module/spring-boot-resttestclient/src/test/java/org/springframework/boot/resttestclient/autoconfigure/RestTestClientAutoConfigurationTests.java index e5bf666f176..620b3c829f2 100644 --- a/module/spring-boot-resttestclient/src/test/java/org/springframework/boot/resttestclient/autoconfigure/RestTestClientAutoConfigurationTests.java +++ b/module/spring-boot-resttestclient/src/test/java/org/springframework/boot/resttestclient/autoconfigure/RestTestClientAutoConfigurationTests.java @@ -75,10 +75,9 @@ class RestTestClientAutoConfigurationTests { org.springframework.boot.test.http.server.LocalTestWebServer$Provider=\ org.springframework.boot.resttestclient.autoconfigure.RestTestClientAutoConfigurationTests$TestLocalTestWebServerProvider """) - void shouldDefineWebTestClientBoundToWebServer() { + void shouldDefineRestTestClientBoundToWebServer() { this.contextRunner.run((context) -> { - assertThat(context).hasSingleBean(RestTestClient.class); - assertThat(context).hasBean("restTestClient"); + assertThat(context).hasSingleBean(RestTestClient.class).hasBean("restTestClient"); RestTestClient client = context.getBean(RestTestClient.class); UriBuilderFactory uiBuilderFactory = (UriBuilderFactory) Extractors .byName("restTestClientBuilder.restClientBuilder.uriBuilderFactory") @@ -97,6 +96,7 @@ class RestTestClientAutoConfigurationTests { } + @SuppressWarnings("unused") static class TestLocalTestWebServerProvider implements LocalTestWebServer.Provider { @Override diff --git a/module/spring-boot-resttestclient/src/test/java/org/springframework/boot/resttestclient/autoconfigure/TestRestTemplateAutoConfigurationTests.java b/module/spring-boot-resttestclient/src/test/java/org/springframework/boot/resttestclient/autoconfigure/TestRestTemplateAutoConfigurationTests.java new file mode 100644 index 00000000000..378b0ed781e --- /dev/null +++ b/module/spring-boot-resttestclient/src/test/java/org/springframework/boot/resttestclient/autoconfigure/TestRestTemplateAutoConfigurationTests.java @@ -0,0 +1,77 @@ +/* + * Copyright 2012-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.resttestclient.autoconfigure; + +import java.net.URI; + +import org.jspecify.annotations.Nullable; +import org.junit.jupiter.api.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.resttestclient.TestRestTemplate; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; +import org.springframework.boot.test.http.server.LocalTestWebServer; +import org.springframework.boot.test.http.server.LocalTestWebServer.Scheme; +import org.springframework.boot.testsupport.classpath.resources.WithResource; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link TestRestTemplateAutoConfiguration}. + * + * @author Stephane Nicoll + */ +class TestRestTemplateAutoConfigurationTests { + + private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(TestRestTemplateAutoConfiguration.class)); + + @Test + void shouldFailTotRegisterTestRestTemplateWithoutWebServer() { + this.contextRunner.run((context) -> assertThat(context).hasFailed() + .getFailure() + .hasMessageContaining(" No local test web server available")); + } + + @Test + @WithResource(name = "META-INF/spring.factories", + content = """ + org.springframework.boot.test.http.server.LocalTestWebServer$Provider=\ + org.springframework.boot.resttestclient.autoconfigure.TestRestTemplateAutoConfigurationTests$TestLocalTestWebServerProvider + """) + void shouldDefineTestRestTemplateBoundToWebServer() { + this.contextRunner.run((context) -> { + assertThat(context).hasSingleBean(TestRestTemplate.class) + .hasBean("org.springframework.boot.resttestclient.TestRestTemplate"); + TestRestTemplate testRestTemplate = context.getBean(TestRestTemplate.class); + assertThat(testRestTemplate.getRestTemplate().getUriTemplateHandler().expand("/")) + .isEqualTo(URI.create("https://localhost:8182/")); + }); + + } + + @SuppressWarnings("unused") + static class TestLocalTestWebServerProvider implements LocalTestWebServer.Provider { + + @Override + public @Nullable LocalTestWebServer getLocalTestWebServer() { + return LocalTestWebServer.of(Scheme.HTTPS, 8182); + } + + } + +} diff --git a/module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/reactive/context/ReactiveWebServerApplicationContextLocalTestWebServerProvider.java b/module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/reactive/context/ReactiveWebServerApplicationContextLocalTestWebServerProvider.java index 08c6c38c136..4dc0279a98a 100644 --- a/module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/reactive/context/ReactiveWebServerApplicationContextLocalTestWebServerProvider.java +++ b/module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/reactive/context/ReactiveWebServerApplicationContextLocalTestWebServerProvider.java @@ -20,7 +20,7 @@ import org.jspecify.annotations.Nullable; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.boot.test.http.server.LocalTestWebServer; -import org.springframework.boot.test.http.server.LocalTestWebServer.Connection; +import org.springframework.boot.test.http.server.LocalTestWebServer.BaseUriDetails; import org.springframework.boot.test.http.server.LocalTestWebServer.Scheme; import org.springframework.boot.web.server.AbstractConfigurableWebServerFactory; import org.springframework.context.ApplicationContext; @@ -56,7 +56,7 @@ class ReactiveWebServerApplicationContextLocalTestWebServerProvider implements L return LocalTestWebServer.of((isSslEnabled(this.context)) ? Scheme.HTTPS : Scheme.HTTP, () -> { int port = this.context.getEnvironment().getProperty("local.server.port", Integer.class, 8080); String path = this.context.getEnvironment().getProperty("spring.webflux.base-path", ""); - return new Connection(port, path); + return new BaseUriDetails(port, path); }); } diff --git a/module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/servlet/context/ServletWebServerApplicationContextLocalTestWebServerProvider.java b/module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/servlet/context/ServletWebServerApplicationContextLocalTestWebServerProvider.java index 59abf693af7..535b9eda21e 100644 --- a/module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/servlet/context/ServletWebServerApplicationContextLocalTestWebServerProvider.java +++ b/module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/servlet/context/ServletWebServerApplicationContextLocalTestWebServerProvider.java @@ -20,7 +20,7 @@ import org.jspecify.annotations.Nullable; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.boot.test.http.server.LocalTestWebServer; -import org.springframework.boot.test.http.server.LocalTestWebServer.Connection; +import org.springframework.boot.test.http.server.LocalTestWebServer.BaseUriDetails; import org.springframework.boot.test.http.server.LocalTestWebServer.Scheme; import org.springframework.boot.web.server.AbstractConfigurableWebServerFactory; import org.springframework.context.ApplicationContext; @@ -56,7 +56,7 @@ class ServletWebServerApplicationContextLocalTestWebServerProvider implements Lo return LocalTestWebServer.of((isSslEnabled(this.context)) ? Scheme.HTTPS : Scheme.HTTP, () -> { int port = this.context.getEnvironment().getProperty("local.server.port", Integer.class, 8080); String path = this.context.getEnvironment().getProperty("server.servlet.context-path", ""); - return new Connection(port, path); + return new BaseUriDetails(port, path); }); } diff --git a/module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/MockMvcWebClientAutoConfiguration.java b/module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/MockMvcWebClientAutoConfiguration.java index 6f704f356df..5d0e3a4e895 100644 --- a/module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/MockMvcWebClientAutoConfiguration.java +++ b/module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/MockMvcWebClientAutoConfiguration.java @@ -24,11 +24,11 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBooleanProp import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebClient; -import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder; import org.springframework.web.util.DefaultUriBuilderFactory; +import org.springframework.web.util.UriBuilderFactory; /** * Auto-configuration for HtmlUnit {@link WebClient} MockMVC integration. @@ -41,12 +41,18 @@ import org.springframework.web.util.DefaultUriBuilderFactory; @ConditionalOnBooleanProperty(name = "spring.test.mockmvc.webclient.enabled", matchIfMissing = true) public final class MockMvcWebClientAutoConfiguration { + /** + * A {@link UriBuilderFactory} that is suitable for Mock access (i.e. without a + * running web server). + */ + private static final UriBuilderFactory MOCK_URI_BUILDER_FACTORY = new DefaultUriBuilderFactory("http://localhost"); + @Bean @ConditionalOnMissingBean({ WebClient.class, MockMvcWebClientBuilder.class }) @ConditionalOnBean(MockMvc.class) - MockMvcWebClientBuilder mockMvcWebClientBuilder(MockMvc mockMvc, ApplicationContext applicationContext) { + MockMvcWebClientBuilder mockMvcWebClientBuilder(MockMvc mockMvc) { return MockMvcWebClientBuilder.mockMvcSetup(mockMvc) - .withDelegate(new UriBuilderFactoryWebClient(new DefaultUriBuilderFactory("http://localhost"))); + .withDelegate(new UriBuilderFactoryWebClient(MOCK_URI_BUILDER_FACTORY)); } @Bean diff --git a/module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/MockMvcWebDriverAutoConfiguration.java b/module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/MockMvcWebDriverAutoConfiguration.java index 5dea68aa157..ca455a89300 100644 --- a/module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/MockMvcWebDriverAutoConfiguration.java +++ b/module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/MockMvcWebDriverAutoConfiguration.java @@ -27,11 +27,11 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBooleanProp import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebConnectionHtmlUnitDriver; -import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder; import org.springframework.web.util.DefaultUriBuilderFactory; +import org.springframework.web.util.UriBuilderFactory; /** * Auto-configuration for Selenium {@link WebDriver} MockMVC integration. @@ -44,13 +44,19 @@ import org.springframework.web.util.DefaultUriBuilderFactory; @ConditionalOnBooleanProperty(name = "spring.test.mockmvc.webdriver.enabled", matchIfMissing = true) public final class MockMvcWebDriverAutoConfiguration { + /** + * A {@link UriBuilderFactory} that is suitable for Mock access (i.e. without a + * running web server). + */ + private static final UriBuilderFactory MOCK_URI_BUILDER_FACTORY = new DefaultUriBuilderFactory("http://localhost"); + @Bean @ConditionalOnMissingBean({ WebDriver.class, MockMvcHtmlUnitDriverBuilder.class }) @ConditionalOnBean(MockMvc.class) - MockMvcHtmlUnitDriverBuilder mockMvcHtmlUnitDriverBuilder(MockMvc mockMvc, ApplicationContext applicationContext) { + MockMvcHtmlUnitDriverBuilder mockMvcHtmlUnitDriverBuilder(MockMvc mockMvc) { return MockMvcHtmlUnitDriverBuilder.mockMvcSetup(mockMvc) - .withDelegate(new UriBuilderFactoryWebConnectionHtmlUnitDriver( - new DefaultUriBuilderFactory("http://localhost"), BrowserVersion.CHROME)); + .withDelegate( + new UriBuilderFactoryWebConnectionHtmlUnitDriver(MOCK_URI_BUILDER_FACTORY, BrowserVersion.CHROME)); } @Bean diff --git a/module/spring-boot-webtestclient/src/test/java/org/springframework/boot/webflux/test/autoconfigure/WebTestClientAutoConfigurationTests.java b/module/spring-boot-webtestclient/src/test/java/org/springframework/boot/webflux/test/autoconfigure/WebTestClientAutoConfigurationTests.java index 7cda9e60553..9129ded5ad4 100644 --- a/module/spring-boot-webtestclient/src/test/java/org/springframework/boot/webflux/test/autoconfigure/WebTestClientAutoConfigurationTests.java +++ b/module/spring-boot-webtestclient/src/test/java/org/springframework/boot/webflux/test/autoconfigure/WebTestClientAutoConfigurationTests.java @@ -225,6 +225,7 @@ class WebTestClientAutoConfigurationTests { } + @SuppressWarnings("unused") static class TestLocalTestWebServerProvider implements LocalTestWebServer.Provider { @Override diff --git a/smoke-test/spring-boot-smoke-test-actuator-custom-security/src/test/java/smoketest/actuator/customsecurity/AbstractSampleActuatorCustomSecurityTests.java b/smoke-test/spring-boot-smoke-test-actuator-custom-security/src/test/java/smoketest/actuator/customsecurity/AbstractSampleActuatorCustomSecurityTests.java index dbaf58522a8..770534b42a8 100644 --- a/smoke-test/spring-boot-smoke-test-actuator-custom-security/src/test/java/smoketest/actuator/customsecurity/AbstractSampleActuatorCustomSecurityTests.java +++ b/smoke-test/spring-boot-smoke-test-actuator-custom-security/src/test/java/smoketest/actuator/customsecurity/AbstractSampleActuatorCustomSecurityTests.java @@ -200,7 +200,7 @@ abstract class AbstractSampleActuatorCustomSecurityTests { } private TestRestTemplate configure(TestRestTemplate restTemplate) { - LocalTestWebServer localTestWebServer = LocalTestWebServer.getRequired(getApplicationContext()); + LocalTestWebServer localTestWebServer = LocalTestWebServer.obtain(getApplicationContext()); restTemplate.setUriTemplateHandler(localTestWebServer.uriBuilderFactory()); return restTemplate; } diff --git a/smoke-test/spring-boot-smoke-test-actuator-custom-security/src/test/java/smoketest/actuator/customsecurity/CorsSampleActuatorApplicationTests.java b/smoke-test/spring-boot-smoke-test-actuator-custom-security/src/test/java/smoketest/actuator/customsecurity/CorsSampleActuatorApplicationTests.java index 683a231989d..c32cb39606e 100644 --- a/smoke-test/spring-boot-smoke-test-actuator-custom-security/src/test/java/smoketest/actuator/customsecurity/CorsSampleActuatorApplicationTests.java +++ b/smoke-test/spring-boot-smoke-test-actuator-custom-security/src/test/java/smoketest/actuator/customsecurity/CorsSampleActuatorApplicationTests.java @@ -54,7 +54,7 @@ class CorsSampleActuatorApplicationTests { @BeforeEach void setUp() { RestTemplateBuilder builder = new RestTemplateBuilder(); - LocalTestWebServer localTestWebServer = LocalTestWebServer.getRequired(this.applicationContext); + LocalTestWebServer localTestWebServer = LocalTestWebServer.obtain(this.applicationContext); builder = builder.uriTemplateHandler(localTestWebServer.uriBuilderFactory()); this.testRestTemplate = new TestRestTemplate(builder); } diff --git a/smoke-test/spring-boot-smoke-test-actuator-extension/src/test/java/smoketest/actuator/extension/SampleActuatorExtensionApplicationTests.java b/smoke-test/spring-boot-smoke-test-actuator-extension/src/test/java/smoketest/actuator/extension/SampleActuatorExtensionApplicationTests.java index 4d93560e4d2..70a8ec21167 100644 --- a/smoke-test/spring-boot-smoke-test-actuator-extension/src/test/java/smoketest/actuator/extension/SampleActuatorExtensionApplicationTests.java +++ b/smoke-test/spring-boot-smoke-test-actuator-extension/src/test/java/smoketest/actuator/extension/SampleActuatorExtensionApplicationTests.java @@ -65,7 +65,7 @@ class SampleActuatorExtensionApplicationTests { void healthExtensionWithAuthHeader() { TestRestTemplate restTemplate = new TestRestTemplate( this.restTemplateBuilder.defaultHeader("Authorization", "Bearer secret")); - LocalTestWebServer localTestWebServer = LocalTestWebServer.getRequired(this.applicationContext); + LocalTestWebServer localTestWebServer = LocalTestWebServer.obtain(this.applicationContext); restTemplate.setUriTemplateHandler(localTestWebServer.uriBuilderFactory()); ResponseEntity entity = restTemplate.getForEntity("/myextension/health", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); diff --git a/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/CorsSampleActuatorApplicationTests.java b/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/CorsSampleActuatorApplicationTests.java index 560b83f01f5..c2a2fe31e3b 100644 --- a/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/CorsSampleActuatorApplicationTests.java +++ b/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/CorsSampleActuatorApplicationTests.java @@ -54,7 +54,7 @@ class CorsSampleActuatorApplicationTests { @BeforeEach void setUp() { RestTemplateBuilder builder = new RestTemplateBuilder(); - LocalTestWebServer localTestWebServer = LocalTestWebServer.getRequired(this.applicationContext); + LocalTestWebServer localTestWebServer = LocalTestWebServer.obtain(this.applicationContext); builder = builder.uriTemplateHandler(localTestWebServer.uriBuilderFactory()); this.testRestTemplate = new TestRestTemplate(builder); }