Commit 53d7fd5a authored by Maciej Walkowiak's avatar Maciej Walkowiak Committed by Andy Wilkinson

Add constructor to TestRestTemplate that takes a RestTemplateBuilder

Closes gh-6706
See gh-6702
parent 6cb18835
...@@ -82,6 +82,19 @@ public class TestRestTemplate { ...@@ -82,6 +82,19 @@ public class TestRestTemplate {
private final RestTemplate restTemplate; private final RestTemplate restTemplate;
/**
* Create a new {@link TestRestTemplate} instance.
* @param restTemplateBuilder builder used to configure underlying {@link RestTemplate}
*/
public TestRestTemplate(RestTemplateBuilder restTemplateBuilder) {
this(buildRestTemplate(restTemplateBuilder));
}
private static RestTemplate buildRestTemplate(RestTemplateBuilder restTemplateBuilder) {
Assert.notNull(restTemplateBuilder, "RestTemplateBuilder must not be null");
return restTemplateBuilder.build();
}
/** /**
* Create a new {@link TestRestTemplate} instance. * Create a new {@link TestRestTemplate} instance.
* @param httpClientOptions client options to use if the Apache HTTP Client is used * @param httpClientOptions client options to use if the Apache HTTP Client is used
......
...@@ -24,6 +24,7 @@ import org.junit.Test; ...@@ -24,6 +24,7 @@ import org.junit.Test;
import org.springframework.boot.test.web.client.TestRestTemplate.CustomHttpComponentsClientHttpRequestFactory; import org.springframework.boot.test.web.client.TestRestTemplate.CustomHttpComponentsClientHttpRequestFactory;
import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption; import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.InterceptingClientHttpRequestFactory; import org.springframework.http.client.InterceptingClientHttpRequestFactory;
...@@ -33,6 +34,7 @@ import org.springframework.web.client.RestOperations; ...@@ -33,6 +34,7 @@ import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
/** /**
...@@ -43,6 +45,15 @@ import static org.mockito.Mockito.mock; ...@@ -43,6 +45,15 @@ import static org.mockito.Mockito.mock;
*/ */
public class TestRestTemplateTests { public class TestRestTemplateTests {
@Test
public void fromRestTemplateBuilder() {
RestTemplateBuilder builder = mock(RestTemplateBuilder.class);
RestTemplate delegate = new RestTemplate();
given(builder.build()).willReturn(delegate);
assertThat(new TestRestTemplate(builder).getRestTemplate())
.isEqualTo(delegate);
}
@Test @Test
public void simple() { public void simple() {
// The Apache client is on the classpath so we get the fully-fledged factory // The Apache client is on the classpath so we get the fully-fledged factory
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment