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.
TestRestTemplate for full integration in 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.
/hello endpoint and checks the response status and body.@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()); } }
newItem and expects a MyObject response.ResponseEntity<MyObject> response = restTemplate.postForEntity("/api/items", newItem, MyObject.class);
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.
@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()); } }
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.
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.
Practice
TestRestTemplate in a Spring Boot application?Solution
Step 1: Understand TestRestTemplate's role
TestRestTemplate is designed to send HTTP requests to the running Spring Boot app during tests.Step 2: Compare with other testing tools
Unlike mocks or stubs, it tests the full app stack by making real HTTP calls.Final Answer:
To perform real HTTP requests for full integration testing -> Option AQuick Check:
TestRestTemplate = real HTTP requests [OK]
- Confusing TestRestTemplate with mocking tools
- Thinking it replaces application context
- Assuming it auto-generates docs
TestRestTemplate with a random port in a Spring Boot test?Solution
Step 1: Identify webEnvironment for real HTTP tests
Using RANDOM_PORT starts the server on a random port for real HTTP calls.Step 2: Understand other options
NONE disables web environment, MOCK uses mock servlet environment, DEFINED_PORT uses fixed port.Final Answer:
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -> Option CQuick Check:
RANDOM_PORT enables real HTTP server [OK]
- Using MOCK disables real HTTP server
- Choosing NONE disables web environment
- Assuming DEFINED_PORT is random
TestRestTemplate:
var response = restTemplate.getForEntity("/api/greet", String.class);
System.out.println(response.getStatusCodeValue());
System.out.println(response.getBody());
If the endpoint /api/greet returns HTTP 200 and body "Hello!", what will be printed?Solution
Step 1: Understand getForEntity behavior
It sends GET request and returns response with status and body.Step 2: Match expected endpoint response
The endpoint returns 200 status and "Hello!" body, so these print exactly.Final Answer:
200\nHello! -> Option DQuick Check:
Status 200 and body "Hello!" printed [OK]
- Assuming 404 if endpoint not found in test
- Expecting null body when response is valid
- Confusing status code with body content
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testGetUser() {
var response = restTemplate.getForEntity("/users/1", User.class);
assertEquals(200, response.getStatusCodeValue());
}
If the test fails with a NullPointerException on restTemplate.getForEntity, what is the most likely cause?Solution
Step 1: Identify cause of NullPointerException
NullPointerException on restTemplate means it was never set (null).Step 2: Check test setup annotations
Without @SpringBootTest, Spring does not create context or inject beans like TestRestTemplate.Final Answer:
TestRestTemplate was not injected because @SpringBootTest annotation is missing -> Option BQuick Check:
Missing @SpringBootTest causes null injection [OK]
- Blaming missing endpoint for NullPointerException
- Assuming serialization error causes NPE
- Forgetting to add @Test annotation
TestRestTemplate that posts JSON data to /api/items and verifies the created item's ID is returned. Which code snippet correctly performs this test?Solution
Step 1: Identify correct HTTP method for creating resource
POST is used to create new items, so postForEntity is appropriate.Step 2: Verify response status and body
201 status means created; response body should contain new item's ID, so assertNotNull on getId() is correct.Final Answer:
Code snippet using postForEntity with POST and checking 201 and ID -> Option AQuick Check:
POST + 201 + returned ID = correct test [OK]
- Using GET instead of POST for creation
- Expecting 404 instead of 201 status
- Using postForObject without status check
Ready to prove it?
Pass the test and this pattern moves unknown
