Challenge - 5 Problems
TestRestTemplate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the test result of this TestRestTemplate GET request?
Consider this JUnit test using TestRestTemplate to call a REST API endpoint that returns a JSON with a message.
What will be the value of
What will be the value of
response.getBody() after this test runs?JUnit
ResponseEntity<String> response = testRestTemplate.getForEntity("/api/greet", String.class); String body = response.getBody();
Attempts:
2 left
💡 Hint
The API returns a JSON string with a message field.
✗ Incorrect
The getForEntity method returns the full JSON string as a String body, including quotes and escaped characters.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies the HTTP status code 200?
You want to check that the response status code from TestRestTemplate is HTTP 200 OK.
Which assertion is correct in JUnit 5?
Which assertion is correct in JUnit 5?
JUnit
ResponseEntity<String> response = testRestTemplate.getForEntity("/api/data", String.class);
Attempts:
2 left
💡 Hint
HttpStatus.OK is an enum constant representing 200.
✗ Incorrect
The getStatusCode() returns an HttpStatus enum, so comparing with HttpStatus.OK is correct.
❓ locator
advanced2:00remaining
Which URI template is correct for a path variable in TestRestTemplate?
You want to call an API endpoint with a path variable userId using TestRestTemplate.
Which URI template and call is correct?
Which URI template and call is correct?
Attempts:
2 left
💡 Hint
TestRestTemplate uses curly braces {} for path variables.
✗ Incorrect
The correct syntax for path variables is using {variableName} in the URI template.
🔧 Debug
advanced2:00remaining
Why does this POST request test fail with 415 Unsupported Media Type?
This test sends a POST request with a JSON body but fails with HTTP 415 error.
What is the likely cause?
What is the likely cause?
JUnit
HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<String> request = new HttpEntity<>("{\"name\":\"Test\"}", headers); ResponseEntity<String> response = testRestTemplate.postForEntity("/api/users", request, String.class);
Attempts:
2 left
💡 Hint
415 means the server does not support the media type sent.
✗ Incorrect
If the server expects a different Content-Type, it rejects application/json with 415.
❓ framework
expert3:00remaining
How to configure TestRestTemplate to use Basic Authentication in a Spring Boot test?
You want to test a secured API endpoint that requires Basic Auth using TestRestTemplate.
Which code snippet correctly configures TestRestTemplate with username 'user' and password 'pass'?
Which code snippet correctly configures TestRestTemplate with username 'user' and password 'pass'?
Attempts:
2 left
💡 Hint
TestRestTemplate has a constructor that accepts username and password.
✗ Incorrect
The simplest way is to use the constructor with username and password parameters.