Challenge - 5 Problems
TestRestTemplate Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this TestRestTemplate GET request?
Given a Spring Boot REST controller that returns a JSON object {"message": "Hello World"} at endpoint /greet, what will this TestRestTemplate call return?
Spring Boot
ResponseEntity<String> response = testRestTemplate.getForEntity("/greet", String.class); String body = response.getBody(); System.out.println(body);
Attempts:
2 left
💡 Hint
Remember the controller returns a JSON object with key 'message'.
✗ Incorrect
The controller returns a JSON object with key 'message' and value 'Hello World'. The TestRestTemplate getForEntity returns the raw JSON string in the body.
❓ state_output
intermediate2:00remaining
What is the HTTP status code after a successful POST with TestRestTemplate?
A Spring Boot controller accepts POST requests at /items and returns ResponseEntity with status CREATED (201). What status code will testRestTemplate.postForEntity("/items", newItem, Item.class).getStatusCodeValue() return?
Spring Boot
ResponseEntity<Item> response = testRestTemplate.postForEntity("/items", newItem, Item.class); int statusCode = response.getStatusCodeValue(); System.out.println(statusCode);
Attempts:
2 left
💡 Hint
Check the controller's ResponseEntity status for POST.
✗ Incorrect
The controller returns ResponseEntity with status CREATED (201), so the TestRestTemplate response status code is 201.
📝 Syntax
advanced2:00remaining
Which TestRestTemplate call correctly sends a PUT request with a JSON body?
You want to update an item at /items/5 using TestRestTemplate. Which option correctly sends a PUT request with the updated item object?
Attempts:
2 left
💡 Hint
PUT requests with body often use exchange method with HttpEntity.
✗ Incorrect
Option A uses exchange with HttpMethod.PUT and HttpEntity wrapping the updatedItem, which is the correct way to send a PUT with body. Option A is valid but does not return a response. Option A is invalid because put method does not take a response type. Option A uses POST instead of PUT.
🔧 Debug
advanced2:00remaining
Why does this TestRestTemplate call throw a 404 error?
Given this code: ResponseEntity response = testRestTemplate.getForEntity("/api/data", String.class); The server returns 404 Not Found. What is the most likely cause?
Attempts:
2 left
💡 Hint
404 means resource not found on server.
✗ Incorrect
A 404 error means the requested URL is not mapped to any controller method. Other options cause different errors or exceptions.
🧠 Conceptual
expert3:00remaining
What is the main advantage of using TestRestTemplate in full integration tests?
Why do developers prefer TestRestTemplate for full integration testing of Spring Boot REST APIs instead of mocking controllers?
Attempts:
2 left
💡 Hint
Think about what full integration means versus unit testing.
✗ Incorrect
TestRestTemplate sends real HTTP requests to a running Spring Boot server, testing serialization, filters, security, and controller logic together. Mocking skips many layers.