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:
parent
eec3eed5f5
commit
2be0c46562
|
@ -46,7 +46,7 @@ public class SpockTestRestTemplateExample {
|
||||||
Environment environment) {
|
Environment environment) {
|
||||||
RestTemplateBuilder builder = builderProvider.getIfAvailable();
|
RestTemplateBuilder builder = builderProvider.getIfAvailable();
|
||||||
TestRestTemplate template = builder == null ? new TestRestTemplate()
|
TestRestTemplate template = builder == null ? new TestRestTemplate()
|
||||||
: new TestRestTemplate(builder.build());
|
: new TestRestTemplate(builder);
|
||||||
template.setUriTemplateHandler(new LocalHostUriTemplateHandler(environment));
|
template.setUriTemplateHandler(new LocalHostUriTemplateHandler(environment));
|
||||||
return template;
|
return template;
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,7 +94,7 @@ public class TestRestTemplate {
|
||||||
* @since 1.4.1
|
* @since 1.4.1
|
||||||
*/
|
*/
|
||||||
public TestRestTemplate(RestTemplateBuilder restTemplateBuilder) {
|
public TestRestTemplate(RestTemplateBuilder restTemplateBuilder) {
|
||||||
this(buildRestTemplate(restTemplateBuilder));
|
this(restTemplateBuilder, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -113,14 +113,24 @@ public class TestRestTemplate {
|
||||||
*/
|
*/
|
||||||
public TestRestTemplate(String username, String password,
|
public TestRestTemplate(String username, String password,
|
||||||
HttpClientOption... httpClientOptions) {
|
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) {
|
HttpClientOption... httpClientOptions) {
|
||||||
Assert.notNull(restTemplate, "RestTemplate must not be null");
|
Assert.notNull(restTemplate, "RestTemplate must not be null");
|
||||||
this.httpClientOptions = httpClientOptions;
|
this.httpClientOptions = httpClientOptions;
|
||||||
|
|
|
@ -143,7 +143,7 @@ class TestRestTemplateTestContextCustomizer implements ContextCustomizer {
|
||||||
throws BeansException {
|
throws BeansException {
|
||||||
RestTemplateBuilder builder = getRestTemplateBuilder(applicationContext);
|
RestTemplateBuilder builder = getRestTemplateBuilder(applicationContext);
|
||||||
boolean sslEnabled = isSslEnabled(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);
|
sslEnabled ? SSL_OPTIONS : DEFAULT_OPTIONS);
|
||||||
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(
|
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(
|
||||||
applicationContext.getEnvironment(), sslEnabled ? "https" : "http");
|
applicationContext.getEnvironment(), sslEnabled ? "https" : "http");
|
||||||
|
|
|
@ -85,7 +85,7 @@ public class TestRestTemplateTests {
|
||||||
@Test
|
@Test
|
||||||
public void getRootUriRootUriSetViaRestTemplateBuilder() {
|
public void getRootUriRootUriSetViaRestTemplateBuilder() {
|
||||||
String rootUri = "http://example.com";
|
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);
|
assertThat(new TestRestTemplate(delegate).getRootUri()).isEqualTo(rootUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,7 +127,9 @@ public class TestRestTemplateTests {
|
||||||
RestTemplate delegate = mock(RestTemplate.class);
|
RestTemplate delegate = mock(RestTemplate.class);
|
||||||
given(delegate.getUriTemplateHandler())
|
given(delegate.getUriTemplateHandler())
|
||||||
.willReturn(new DefaultUriBuilderFactory());
|
.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() {
|
ReflectionUtils.doWithMethods(RestOperations.class, new MethodCallback() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -338,9 +340,8 @@ public class TestRestTemplateTests {
|
||||||
.create("http://localhost:8080/a/b/c.txt?param=%7Bsomething%7D");
|
.create("http://localhost:8080/a/b/c.txt?param=%7Bsomething%7D");
|
||||||
given(requestFactory.createRequest(eq(absoluteUri), any(HttpMethod.class)))
|
given(requestFactory.createRequest(eq(absoluteUri), any(HttpMethod.class)))
|
||||||
.willReturn(request);
|
.willReturn(request);
|
||||||
RestTemplate delegate = new RestTemplate();
|
TestRestTemplate template = new TestRestTemplate();
|
||||||
TestRestTemplate template = new TestRestTemplate(delegate);
|
template.getRestTemplate().setRequestFactory(requestFactory);
|
||||||
delegate.setRequestFactory(requestFactory);
|
|
||||||
LocalHostUriTemplateHandler uriTemplateHandler = new LocalHostUriTemplateHandler(
|
LocalHostUriTemplateHandler uriTemplateHandler = new LocalHostUriTemplateHandler(
|
||||||
new MockEnvironment());
|
new MockEnvironment());
|
||||||
template.setUriTemplateHandler(uriTemplateHandler);
|
template.setUriTemplateHandler(uriTemplateHandler);
|
||||||
|
|
|
@ -27,14 +27,13 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler;
|
import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler;
|
||||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.RequestEntity;
|
import org.springframework.http.RequestEntity;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@ -55,12 +54,11 @@ public class CorsSampleActuatorApplicationTests {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
RestTemplate restTemplate = new RestTemplate();
|
RestTemplateBuilder builder = new RestTemplateBuilder();
|
||||||
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(
|
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(
|
||||||
this.applicationContext.getEnvironment(), "http");
|
this.applicationContext.getEnvironment(), "http");
|
||||||
restTemplate.setUriTemplateHandler(handler);
|
builder = builder.uriTemplateHandler(handler);
|
||||||
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
|
this.testRestTemplate = new TestRestTemplate(builder);
|
||||||
this.testRestTemplate = new TestRestTemplate(restTemplate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue