About this lesson
REST Assured 404 error response: testing a resource that does not exist
Learn REST AssuredBeginnerStatus Code Validation
How it works
Negative tests check how an API behaves when the request cannot be served. A GET for a record that is not there should return 404 Not Found together with a body that explains why. The practice API sends its errors as JSON with two keys: error, a readable message, and status, the same code repeated as a number.
REST Assured does not treat 404 as an exception. The call returns a normal Response, the content type is still JSON, and jsonPath() reads the error body just like a success body. That makes error paths easy to inspect, but it also means a test only notices a 404 if it looks at the code.
About the assignment
Only users 1 to 5 exist, so asking for user 99 has to fail. You request that missing record, keep the response, and print both the status code and the explanation the server put in the body.
What you will do
- Send a GET for
/users/99and store theResponse, no exception will be thrown. - Read the status code from the response and print it after the label
Status:. - Open the body with
jsonPath()and read theerrorfield as a string. - Print that text after the label
Error:.
Common mistakes
Wrapping the call in try/catch
Nothing is thrown for a 404, so a catch block waiting for one never runs. Read the status from the response instead of waiting for an exception that will not come.
Asserting only the error code
A 404 can come from a mistyped route as well as from a missing record. Checking the message as well, for example with
containsString(), proves the request reached the users endpoint.Expecting 400 for a non-numeric id
This API does not validate the id format. Asking for
/users/abcalso returns 404 with the same style of message, so do not write a test that expects 400 there.
In a real test suite
In JUnit 5, error cases often live in a parameterised test that feeds several bad ids to one method and asserts statusCode(404) plus a body matcher on error. The same pattern covers 400 for missing fields and 401 for missing credentials later in the course.
Related lessons
- Validate 200 OKThe success-path version: assert that
/usersanswers 200. - Handle PUT Validation ErrorAnother error body, this time a 400 from a PUT with a missing field.
- Handle 401 UnauthorizedRead the 401 error body from a protected endpoint called without a token.
To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: GET /users/99
// TODO: Print the status code
// TODO: Print the "error" field from the JSON response
}
}