0
0
Spring Bootframework~5 mins

TestRestTemplate for full integration in Spring Boot

Choose your learning style9 modes available
Introduction

TestRestTemplate helps you test your whole Spring Boot app by making real HTTP calls to your running server. It shows if your app works end-to-end.

You want to check if your REST API endpoints respond correctly.
You need to test how your app handles real HTTP requests and responses.
You want to verify the integration of controllers, services, and repositories together.
You want to test your app with real data and HTTP status codes.
You want to simulate client calls to your Spring Boot app in tests.
Syntax
Spring Boot
@Autowired
private TestRestTemplate restTemplate;

ResponseEntity<Type> response = restTemplate.getForEntity("/api/path", Type.class);

Use @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) to start the server on a random port for testing.

TestRestTemplate is injected automatically by Spring Boot in test classes.

Examples
This example tests a GET request to the /hello endpoint and checks the response status and body.
Spring Boot
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class MyIntegrationTest {

  @Autowired
  private TestRestTemplate restTemplate;

  @Test
  void testGet() {
    ResponseEntity<String> response = restTemplate.getForEntity("/hello", String.class);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals("Hello World", response.getBody());
  }
}
This sends a POST request with newItem and expects a MyObject response.
Spring Boot
ResponseEntity<MyObject> response = restTemplate.postForEntity("/api/items", newItem, MyObject.class);
Sample Program

This full example shows a simple Spring Boot app with a /hello endpoint. The test uses TestRestTemplate to call the endpoint and check the response.

Spring Boot
@SpringBootApplication
public class DemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

@RestController
class HelloController {
  @GetMapping("/hello")
  public String sayHello() {
    return "Hello World";
  }
}

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class HelloControllerIntegrationTest {

  @Autowired
  private TestRestTemplate restTemplate;

  @Test
  void testHelloEndpoint() {
    ResponseEntity<String> response = restTemplate.getForEntity("/hello", String.class);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals("Hello World", response.getBody());
  }
}
OutputSuccess
Important Notes

TestRestTemplate works only in tests with a running Spring Boot server.

Use webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT to avoid port conflicts.

You can test all HTTP methods: GET, POST, PUT, DELETE using TestRestTemplate methods.

Summary

TestRestTemplate lets you test your app like a real client using HTTP calls.

It helps verify your full app integration, not just parts of it.

Use it in Spring Boot tests with a running server on a random port.