title: How to configure Spring TestRestTemplate
body: <p>I have a REST (spring-hateoas) server that I'd like to test with a JUnit test. Therefore I am using an autoinjected <code>TestRestTemplate</code>.</p>
  
  <p>But how do I now add some more configuration to this pre configured TestRestTemplate? I need to configure the rootURI and add interceptors.</p>
  
  <p>Thisi s my JUnit Test class:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @ActiveProfiles("test")
  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)      
  
  public class RestEndpointTests {
    private Logger log = LoggerFactory.getLogger(this.getClass());
  
    @LocalServerPort
    int localServerPort;
  
    @Value(value = "${spring.data.rest.base-path}")   // nice trick to get basePath from application.properties
    String basePath;
  
    @Autowired
    TestRestTemplate client;    //  how to configure client?
  
    [... here are my @Test methods that use client ...]
  }
  </code></pre>
  
  <p><a href="http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-rest-templates-test-utility" rel="noreferrer">The documentation sais that a static <code>@TestConfiguration</code> class can be used.</a> But inside that static class I cannot access <code>localServerPort</code> or <code>basePath</code>:</p>
  
  <pre><code>  @TestConfiguration
    static class Config {
  
      @Bean
      public RestTemplateBuilder restTemplateBuilder() {
        String rootUri = "http://localhost:"+localServerPort+basePath;    // &lt;=== DOES NOT WORK
        log.trace("Creating and configuring RestTemplate for "+rootUri);
        return new RestTemplateBuilder()
          .basicAuthorization(TestFixtures.USER1_EMAIL, TestFixtures.USER1_PWD)
          .errorHandler(new LiquidoTestErrorHandler())
          .requestFactory(new HttpComponentsClientHttpRequestFactory())
          .additionalInterceptors(new LogRequestInterceptor())
          .rootUri(rootUri);
      }
  
    }
  </code></pre>
  
  <p>My most important question: Why doesn't <code>TestRestTemplate</code> take <code>spring.data.rest.base-path</code> from <code>application.properties</code> into account in the first place?  Isn't the idea of beeing complete preconfigured, the whole use case of this wrapper class?</p>
  
  <p>The doc sais</p>
  
  <blockquote>
    <p>If you are using the @SpringBootTest annotation, a TestRestTemplate is
    automatically available and can be @Autowired into you test. If you
    need customizations (for example to adding additional message
    converters) use a RestTemplateBuilder @Bean.</p>
  </blockquote>
  
  <p>How does that look like in a complete Java code example?</p>