Performance: TestRestTemplate for full integration
This affects the speed and responsiveness of full integration tests by simulating real HTTP requests and responses.
Using TestRestTemplate to perform real HTTP calls against a running server in integration tests.Using MockMvc for all tests including full integration tests that require actual HTTP calls and context loading.
| Pattern | Network Simulation | Serialization Cost | Test Execution Time | Verdict |
|---|---|---|---|---|
| MockMvc for all tests | No network simulation | Minimal serialization | Fast (milliseconds) | [X] Bad for full integration |
| TestRestTemplate for full integration | Full network simulation | Full serialization | Slower (seconds) | [OK] Good for realistic integration |
TestRestTemplate in a Spring Boot application?TestRestTemplate with a random port in a Spring Boot test?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?@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?TestRestTemplate that posts JSON data to /api/items and verifies the created item's ID is returned. Which code snippet correctly performs this test?Pass the test and this pattern moves unknown