diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java index 136475ea831d..e8fd5f8dc49d 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java @@ -58,9 +58,23 @@ public LocalHostUriTemplateHandler(Environment environment) { * @since 1.4.1 */ public LocalHostUriTemplateHandler(Environment environment, String scheme) { - super(new DefaultUriBuilderFactory()); + this(environment, scheme, new DefaultUriBuilderFactory()); + } + + /** + * Create a new {@code LocalHostUriTemplateHandler} that will generate URIs with the + * given {@code scheme}, use the given {@code environment} to determine the + * context-path and port and delegate to the given template {@code handler}. + * @param environment the environment used to determine the port + * @param scheme the scheme of the root uri + * @param handler the delegate handler + * @since 2.0.3 + */ + public LocalHostUriTemplateHandler(Environment environment, String scheme, UriTemplateHandler handler) { + super(handler); Assert.notNull(environment, "Environment must not be null"); Assert.notNull(scheme, "Scheme must not be null"); + Assert.notNull(handler, "Handler must not be null"); this.environment = environment; this.scheme = scheme; } diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java index 04f9aab156eb..faa150fd228a 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java @@ -50,6 +50,13 @@ public void createWhenSchemeIsNullShouldThrowException() { new LocalHostUriTemplateHandler(new MockEnvironment(), null); } + @Test + public void createWhenHandlerIsNullShouldThrowException() { + this.thrown.expect(IllegalArgumentException.class); + this.thrown.expectMessage("Handler must not be null"); + new LocalHostUriTemplateHandler(new MockEnvironment(), "http", null); + } + @Test public void getRootUriShouldUseLocalServerPort() { MockEnvironment environment = new MockEnvironment();