Skip to content
CodeKruPro

Validate 200 OK

4/62

Validate 200 OK

The most common status code is 200 OK, meaning the request succeeded.

In REST Assured, you validate status codes in the .then() block:

given().get("/endpoint").then().statusCode(200);

If the status code doesn't match, REST Assured throws an AssertionError and the test fails immediately.

Task:

  1. GET /users and validate status code is 200
  2. Print the confirmation message

Expected output:

GET /users - Status 200 OK

About this lesson

REST Assured validate status code 200: asserting success with statusCode()

Learn REST AssuredBeginnerStatus Code Validation

How it works

Reading a status code and asserting it are different things. then().statusCode(200) compares the reply with the code you expect and throws java.lang.AssertionError when they differ. The message names both numbers, for example "Expected status code <201> but was <200>", under a "1 expectation failed." summary.

Status codes come in families: 2xx success, 3xx redirection, 4xx client errors such as 404, and 5xx server failures. A precise test asserts the exact code the API contract promises. statusCode() also accepts a Hamcrest matcher, so statusCode(lessThan(300)) passes for any success code, and statusLine() checks the whole line including the reason phrase.

About the assignment

The status assertion is the whole test here. You request the user list, let REST Assured confirm the server answered 200 OK, and print a confirmation line that can only be reached once that check has passed.

What you will do

  1. Write a chain that starts with given() and sends a GET to /users.
  2. After then(), call statusCode() with 200.
  3. End the statement there, there is nothing to extract.
  4. On the next line, print the confirmation message exactly as the task shows it.

Common mistakes

  • Printing instead of asserting

    Printing getStatusCode() shows the number but never fails. If the API starts returning 500, the program still finishes normally. Use statusCode() inside then() when the code matters.

  • Stopping at the status code

    A 200 proves the server answered, not that the data is right. Pair the status check with at least one body() assertion on content you care about.

  • Printing the success line first

    If the confirmation is printed before the chain, it appears even when the assertion then fails. Keep the print after the statement so it is only reached on success.

In a real test suite

In a suite, a status assertion usually sits beside a content-type check such as contentType(ContentType.JSON) and a few body matchers. When many endpoints share those expectations, they move into a response specification so each test states them in one call.

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 and validate statusCode(200)
        // TODO: Print "GET /users - Status 200 OK"
    }
}