Skip to content
CodeKruPro

Handle 404 Not Found

5/62

Handle 404 Not Found

404 Not Found means the requested resource doesn't exist. The API returns an error message explaining what went wrong.

Try requesting /users/99 — there is no user with ID 99 (valid IDs are 1-5).

Task:

  1. GET /users/99 and store the Response
  2. Print the status code and the error field from the response body

Expected output:

Status: 404
Error: User with id 99 not found. Valid user IDs: 1-5

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

  1. Send a GET for /users/99 and store the Response, no exception will be thrown.
  2. Read the status code from the response and print it after the label Status:.
  3. Open the body with jsonPath() and read the error field as a string.
  4. 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/abc also 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.

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.

Main.java
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
    }
}