How to add basic auth to Autowired testRestTemplate in SpringBootTest; Spring Boot 1.4
<p>My OAuth integration test before Spring Boot 1.4 looked as follows(updates just to not use deprecated features):</p>

<pre><code>@RunWith(SpringRunner.class)
@SpringBootTest(classes = { ApplicationConfiguration.class }, webEnvironment = WebEnvironment.RANDOM_PORT)
public class OAuth2IntegrationTest {

    @Value("${local.server.port}")
    private int port;

    private static final String CLIENT_NAME = "client";
    private static final String CLIENT_PASSWORD = "123456";

    @Test
    public void testOAuthAccessTokenIsReturned() {
        MultiValueMap&lt;String, String&gt; request = new LinkedMultiValueMap&lt;String, String&gt;();
        request.set("username", "user");
        request.set("password", password);
        request.set("grant_type", "password");
        @SuppressWarnings("unchecked")
        Map&lt;String, Object&gt; token = new TestRestTemplate(CLIENT_NAME, CLIENT_PASSWORD)
            .postForObject("http://localhost:" + port + "/oauth/token", request, Map.class);
        assertNotNull("Wrong response: " + token, token.get("access_token"));
    }
}
</code></pre>

<p>I now want to use Autowired TestRestTemplate as stated here <a href="http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-working-with-random-ports" rel="noreferrer">http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-working-with-random-ports</a></p>

<pre><code>@RunWith(SpringRunner.class)
@SpringBootTest(classes = {
    ApplicationConfiguration.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class OAuth2IntegrationTest {

    private static final String CLIENT_NAME = "client";
    private static final String CLIENT_PASSWORD = "123456";

    @Autowired
    private TestRestTemplate testRestTemplate;

    @Test
    public void testOAuthAccessTokenIsReturned() {
        MultiValueMap&lt;String, String&gt; request = new LinkedMultiValueMap&lt;String, String&gt;();
        request.set("username", "user");
        request.set("password", password);
        request.set("grant_type", "password");
        @SuppressWarnings("unchecked")

        Map&lt;String, Object&gt; token1 = this.testRestTemplate. //how to add basic auth here
        assertNotNull("Wrong response: " + token, token.get("access_token"));
    }
}
</code></pre>

<p>I saw this as the closest way to add auth: </p>

<blockquote>
  <p><a href="https://stackoverflow.com/questions/21998405/spring-4-0-0-basic-authentication-with-resttemplate">Spring 4.0.0 basic authentication with RestTemplate</a></p>
</blockquote>

<p>I want to use the Autowired testRestTemplate to avoid resolving host and ports in my test. Is there a way to do this?</p>