Merge pull request #6706 from Maciej Walkowiak

* gh-6706:
  Polish “Add constructor to TestRestTemplate that takes a RestTemplateBuilder”
  Add constructor to TestRestTemplate that takes a RestTemplateBuilder
pull/6728/head
Andy Wilkinson 8 years ago
commit 53374ec51d

@ -82,6 +82,16 @@ public class TestRestTemplate {
private final RestTemplate restTemplate;
/**
* Create a new {@link TestRestTemplate} instance.
* @param restTemplateBuilder builder used to configure underlying
* {@link RestTemplate}
* @since 1.4.1
*/
public TestRestTemplate(RestTemplateBuilder restTemplateBuilder) {
this(buildRestTemplate(restTemplateBuilder));
}
/**
* Create a new {@link TestRestTemplate} instance.
* @param httpClientOptions client options to use if the Apache HTTP Client is used
@ -117,6 +127,12 @@ public class TestRestTemplate {
this.restTemplate = restTemplate;
}
private static RestTemplate buildRestTemplate(
RestTemplateBuilder restTemplateBuilder) {
Assert.notNull(restTemplateBuilder, "RestTemplateBuilder must not be null");
return restTemplateBuilder.build();
}
private void addAuthentication(RestTemplate restTemplate, String username,
String password) {
if (username == null) {

@ -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,14 @@ 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

Loading…
Cancel
Save