Add Header Value For Spring TestRestTemplate Integration Test

DavidR picture DavidR · Jan 6, 2016 · Viewed 18.7k times · Source

I am using TestRestTemplate for integration testing on our product.

I have one test that looks like this:

@Test
public void testDeviceQuery() {
    ResponseEntity<Page> deviceInfoPage = template.getForEntity(base, Page.class);

    // validation code here
}

This particular request expects a Header value. Can someone please let me know how I can add a header to the TestRestTemplate call?

Answer

Ali Dehghani picture Ali Dehghani · Jan 6, 2016

Update: As of Spring Boot 1.4.0, TestRestTemplate does not extend RestTemplate anymore but still provides the same API as RestTemplate.

TestRestTemplate extends RestTemplate provides the same API as the RestTemplate, so you can use that same API for sending requests. For example:

HttpHeaders headers = new HttpHeaders();
headers.add("your_header", "its_value");
template.exchange(base, HttpMethod.GET, new HttpEntity<>(headers), Page.class);