Remove TestRestTemplate constructors taking template args
As discussed in gh-11872, `TestRestTemplate` constructor variants taking a `RestTemplate` argument are confusing since the main goal of that class is to mutate `RestTemplate`. This commit removes all those constructor variants and replaces them with `RestTemplateBuilder` arguments when possible. Closes gh-11872
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user