diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/test/spock/SpockTestRestTemplateExample.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/test/spock/SpockTestRestTemplateExample.java index cc0e36145ac..8abb2323aa3 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/test/spock/SpockTestRestTemplateExample.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/test/spock/SpockTestRestTemplateExample.java @@ -46,7 +46,7 @@ public class SpockTestRestTemplateExample { Environment environment) { RestTemplateBuilder builder = builderProvider.getIfAvailable(); TestRestTemplate template = builder == null ? new TestRestTemplate() - : new TestRestTemplate(builder.build()); + : new TestRestTemplate(builder); template.setUriTemplateHandler(new LocalHostUriTemplateHandler(environment)); return template; } diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java index 89d9cbe6733..980ca862d2f 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java @@ -94,7 +94,7 @@ public class TestRestTemplate { * @since 1.4.1 */ public TestRestTemplate(RestTemplateBuilder restTemplateBuilder) { - this(buildRestTemplate(restTemplateBuilder)); + this(restTemplateBuilder, null, null); } /** @@ -113,14 +113,24 @@ public class TestRestTemplate { */ public TestRestTemplate(String username, String password, HttpClientOption... httpClientOptions) { - this(new RestTemplate(), username, password, httpClientOptions); + this(new RestTemplateBuilder(), username, password, httpClientOptions); } - public TestRestTemplate(RestTemplate restTemplate) { - this(restTemplate, null, null); + /** + * Create a new {@link TestRestTemplate} instance with the specified credentials. + * @param restTemplateBuilder builder used to configure underlying + * {@link RestTemplate} + * @param username the username to use (or {@code null}) + * @param password the password (or {@code null}) + * @param httpClientOptions client options to use if the Apache HTTP Client is used + * @since 2.0.0 + */ + public TestRestTemplate(RestTemplateBuilder restTemplateBuilder, String username, String password, + HttpClientOption... httpClientOptions) { + this(buildRestTemplate(restTemplateBuilder), username, password, httpClientOptions); } - public TestRestTemplate(RestTemplate restTemplate, String username, String password, + private TestRestTemplate(RestTemplate restTemplate, String username, String password, HttpClientOption... httpClientOptions) { Assert.notNull(restTemplate, "RestTemplate must not be null"); this.httpClientOptions = httpClientOptions; diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplateTestContextCustomizer.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplateTestContextCustomizer.java index 7ba5742537c..7eb350ad87d 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplateTestContextCustomizer.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplateTestContextCustomizer.java @@ -143,7 +143,7 @@ class TestRestTemplateTestContextCustomizer implements ContextCustomizer { throws BeansException { RestTemplateBuilder builder = getRestTemplateBuilder(applicationContext); boolean sslEnabled = isSslEnabled(applicationContext); - TestRestTemplate template = new TestRestTemplate(builder.build(), null, null, + TestRestTemplate template = new TestRestTemplate(builder, null, null, sslEnabled ? SSL_OPTIONS : DEFAULT_OPTIONS); LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler( applicationContext.getEnvironment(), sslEnabled ? "https" : "http"); diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java index 616a957d9cb..43094e656ba 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java @@ -85,7 +85,7 @@ public class TestRestTemplateTests { @Test public void getRootUriRootUriSetViaRestTemplateBuilder() { String rootUri = "http://example.com"; - RestTemplate delegate = new RestTemplateBuilder().rootUri(rootUri).build(); + RestTemplateBuilder delegate = new RestTemplateBuilder().rootUri(rootUri); assertThat(new TestRestTemplate(delegate).getRootUri()).isEqualTo(rootUri); } @@ -127,7 +127,9 @@ public class TestRestTemplateTests { RestTemplate delegate = mock(RestTemplate.class); given(delegate.getUriTemplateHandler()) .willReturn(new DefaultUriBuilderFactory()); - final TestRestTemplate restTemplate = new TestRestTemplate(delegate); + RestTemplateBuilder builder = mock(RestTemplateBuilder.class); + given(builder.build()).willReturn(delegate); + final TestRestTemplate restTemplate = new TestRestTemplate(builder); ReflectionUtils.doWithMethods(RestOperations.class, new MethodCallback() { @Override @@ -338,9 +340,8 @@ public class TestRestTemplateTests { .create("http://localhost:8080/a/b/c.txt?param=%7Bsomething%7D"); given(requestFactory.createRequest(eq(absoluteUri), any(HttpMethod.class))) .willReturn(request); - RestTemplate delegate = new RestTemplate(); - TestRestTemplate template = new TestRestTemplate(delegate); - delegate.setRequestFactory(requestFactory); + TestRestTemplate template = new TestRestTemplate(); + template.getRestTemplate().setRequestFactory(requestFactory); LocalHostUriTemplateHandler uriTemplateHandler = new LocalHostUriTemplateHandler( new MockEnvironment()); template.setUriTemplateHandler(uriTemplateHandler); diff --git a/spring-boot-samples/spring-boot-sample-actuator-custom-security/src/test/java/sample/actuator/customsecurity/CorsSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator-custom-security/src/test/java/sample/actuator/customsecurity/CorsSampleActuatorApplicationTests.java index dd70c468a86..0e04448553a 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-custom-security/src/test/java/sample/actuator/customsecurity/CorsSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator-custom-security/src/test/java/sample/actuator/customsecurity/CorsSampleActuatorApplicationTests.java @@ -27,14 +27,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler; import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.ApplicationContext; import org.springframework.http.HttpStatus; import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.web.client.RestTemplate; import static org.assertj.core.api.Assertions.assertThat; @@ -55,12 +54,11 @@ public class CorsSampleActuatorApplicationTests { @Before public void setUp() { - RestTemplate restTemplate = new RestTemplate(); + RestTemplateBuilder builder = new RestTemplateBuilder(); LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler( this.applicationContext.getEnvironment(), "http"); - restTemplate.setUriTemplateHandler(handler); - restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory()); - this.testRestTemplate = new TestRestTemplate(restTemplate); + builder = builder.uriTemplateHandler(handler); + this.testRestTemplate = new TestRestTemplate(builder); } @Test