0
0
JUnittesting~20 mins

TestRestTemplate for API testing in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TestRestTemplate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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 response.getBody() after this test runs?
JUnit
ResponseEntity<String> response = testRestTemplate.getForEntity("/api/greet", String.class);
String body = response.getBody();
A"{message:Hello World}"
Bnull
C"Hello World"
D"{\"message\":\"Hello World\"}"
Attempts:
2 left
💡 Hint
The API returns a JSON string with a message field.
assertion
intermediate
2: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?
JUnit
ResponseEntity<String> response = testRestTemplate.getForEntity("/api/data", String.class);
AassertEquals(HttpStatus.OK, response.getStatusCode());
BassertTrue(response.getStatusCode() == 200);
CassertEquals(200, response.getStatusCodeValue());
DassertEquals("200", response.getStatusCode().toString());
Attempts:
2 left
💡 Hint
HttpStatus.OK is an enum constant representing 200.
locator
advanced
2: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?
AtestRestTemplate.getForEntity("/users/:userId", String.class, 42);
BtestRestTemplate.getForEntity("/users/{userId}", String.class, 42);
CtestRestTemplate.getForEntity("/users/$userId", String.class, 42);
DtestRestTemplate.getForEntity("/users/?userId=42", String.class);
Attempts:
2 left
💡 Hint
TestRestTemplate uses curly braces {} for path variables.
🔧 Debug
advanced
2: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?
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);
AThe HttpEntity is missing the Accept header.
BThe JSON string is malformed and causes parsing error.
CThe server expects a different Content-Type header than application/json.
DThe testRestTemplate instance is not initialized properly.
Attempts:
2 left
💡 Hint
415 means the server does not support the media type sent.
framework
expert
3: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'?
ATestRestTemplate testRestTemplate = new TestRestTemplate("user", "pass");
B
TestRestTemplate testRestTemplate = new TestRestTemplate();
testRestTemplate.getRestTemplate().getInterceptors().add(new BasicAuthenticationInterceptor("user", "pass"));
C
TestRestTemplate testRestTemplate = new TestRestTemplate();
testRestTemplate.setBasicAuth("user", "pass");
D
TestRestTemplate testRestTemplate = new TestRestTemplate();
testRestTemplate.getRestTemplate().setBasicAuth("user", "pass");
Attempts:
2 left
💡 Hint
TestRestTemplate has a constructor that accepts username and password.