Add constructor to TestRestTemplate that takes a RestTemplateBuilder

Closes gh-6706
See gh-6702
This commit is contained in:
Maciej Walkowiak 2016-08-20 22:46:34 +02:00 committed by Andy Wilkinson
parent 6cb18835b9
commit 53d7fd5aab
2 changed files with 24 additions and 0 deletions

View File

@ -82,6 +82,19 @@ public class TestRestTemplate {
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.
* @param httpClientOptions client options to use if the Apache HTTP Client is used

View File

@ -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.HttpClientOption;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
@ -33,6 +34,7 @@ import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
@ -43,6 +45,15 @@ import static org.mockito.Mockito.mock;
*/
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
public void simple() {
// The Apache client is on the classpath so we get the fully-fledged factory