A. The URL is missing a user ID, causing a 404 error
B. The assertion should check for 404 instead of 200
C. TestRestTemplate cannot be autowired
D. ResponseEntity<String> is not a valid return type
Solution
Step 1: Analyze the request URL
The URL "/api/users/" lacks a specific user ID, so the endpoint may not exist or return a list, not a single user.
Step 2: Understand the assertion failure cause
Expecting status 200 for a missing user ID endpoint may cause a 404 error, failing the test.
Final Answer:
The URL is missing a user ID, causing a 404 error -> Option A
Quick Check:
Missing ID in URL causes 404, not 200 [OK]
Hint: Check URL completeness before asserting status [OK]
Common Mistakes:
Assuming URL without ID returns 200
Wrong assertion status code
Thinking autowiring is the issue
5. You want to test a POST API endpoint /api/users that creates a user and returns HTTP 201 with the created user JSON. Which TestRestTemplate method and assertion combination correctly tests this?
hard
A. Use delete with user ID, then assert status 204
B. Use getForEntity with user ID, then assert status 200
C. Use exchange with GET method, then assert status 404
D. Use postForEntity with user object, then assert status 201 and response body contains user data
Solution
Step 1: Identify correct HTTP method for creation
POST is used to create resources, so postForEntity is appropriate for sending the user object.
Step 2: Validate expected response and assertions
The API returns 201 Created with user JSON, so assert status 201 and check response body contains user data.
Final Answer:
Use postForEntity with user object, then assert status 201 and response body contains user data -> Option D
Quick Check:
POST + postForEntity + assert 201 + check body [OK]
Hint: POST creation uses postForEntity and assert 201 [OK]
Common Mistakes:
Using GET instead of POST for creation
Asserting wrong status codes
Ignoring response body validation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.