Configure TestRestTemplate using builder
Update SpringBootTestContextCustomizer to create the TestRestTemplate using the RestTemplateBuilder whenever possible. Fixes gh-5509
This commit is contained in:
@@ -60,6 +60,10 @@ public abstract class AbstractSpringBootTestEmbeddedWebEnvironmentTests {
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
public TestRestTemplate getRestTemplate() {
|
||||
return this.restTemplate;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runAndTestHttpEndpoint() {
|
||||
assertThat(this.port).isNotEqualTo(8080).isNotEqualTo(0);
|
||||
|
||||
@@ -16,15 +16,21 @@
|
||||
|
||||
package org.springframework.boot.test.context;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringBootTest} configured with {@link WebEnvironment#RANDOM_PORT}.
|
||||
*
|
||||
@@ -37,11 +43,28 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
public class SpringBootTestWebEnvironmentRandomPortTests
|
||||
extends AbstractSpringBootTestEmbeddedWebEnvironmentTests {
|
||||
|
||||
@Test
|
||||
public void testRestTemplateShouldUseBuilder() throws Exception {
|
||||
assertThat(getRestTemplate().getRestTemplate().getMessageConverters())
|
||||
.hasAtLeastOneElementOfType(MyConverter.class);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@RestController
|
||||
protected static class Config extends AbstractConfig {
|
||||
|
||||
@Bean
|
||||
public RestTemplateBuilder restTemplateBuilder() {
|
||||
return new RestTemplateBuilder()
|
||||
.additionalMessageConverters(new MyConverter());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class MyConverter extends StringHttpMessageConverter {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user