mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Add constructor to TestRestTemplate that takes a RestTemplateBuilder
Closes gh-6706 See gh-6702
This commit is contained in:
parent
6cb18835b9
commit
53d7fd5aab
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user