Skip to the page

API testing · core

API checks: status codes and REST Assured

An API test checks the status code, the body, the schema, the headers and the side effects, not only that the call worked.

The notes

What you must know

  • 201 Created for a new resource, 400 or 422 for validation (a team choice, but be consistent), 401 for missing or invalid credentials, 403 for a known user without permission, 404 Not Found, 409 Conflict for duplicates, 429 Too Many Requests, 500 for an unhandled error, 502, 503, 504 for gateway and upstream problems.
  • 200 with {"error": ...} in the body is a bug or a contract problem. Clients, monitoring and retries depend on status codes.
  • GET, PUT and DELETE are idempotent: repeating gives the same result. POST is not. 204 has no body.
  • Beyond status: body values, schema, headers (Content-Type, caching, security headers), response time, side effects such as a database row or an event, and error bodies without stack traces.
  • REST Assured style: given() the request spec, when() post, then() statusCode(201), body checks, schema check with json-schema-validator, and extract() the id.
  • Use a POJO or record serialised by Jackson, not hand-built JSON. Use RequestSpecBuilder and ResponseSpecBuilder for shared parts, and log().ifValidationFails().
  • Use unique data (an email with a UUID) and clean up after the test. A response-time check is a smoke signal, not a performance test.
  • Postman collections run in CI with Newman or with the Postman CLI. Keep environment files without secrets, and pass secrets from CI variables.

The red pen

Common traps: mistake → fix

  • Mistake: 401 and 403 are the same

    Fix: 401 is not authenticated, 403 is authenticated but not allowed.

  • Mistake: Checking the status code is enough

    Fix: a wrong body or schema still passes.

  • Mistake: An if (status == 200) around assertions is safe

    Fix: any other status skips all checks and the test passes.

  • Mistake: A try/catch around the test keeps the suite running

    Fix: it swallows errors and makes the test always pass.

Easy to hard

Questions interviewers ask, easy to hard

  1. Which status code do you expect for a duplicate email? For a missing token? For a user without permission?
  2. What do you check in an API response besides the status code?
  3. Which HTTP methods are idempotent?
  4. How do you run a Postman collection in CI?

Practise

Practise with these questions

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview