DavidR DavidR - 5 months ago 79
Java Question

Add Header Value For Spring TestRestTemplate Integration Test

I am using

for integration testing on our product.

I have one test that looks like this:

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


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);