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
- Write a chain that starts with
given()and sends a GET to/users. - After
then(), callstatusCode()with 200. - End the statement there, there is nothing to extract.
- 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. UsestatusCode()insidethen()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.
Related lessons
- Handle 404 Not FoundSee a failing request: the 404 status and error text for a missing user.
- Validate 201 CreatedCheck a 201 Created after sending a POST.
- Basic Response SpecificationReuse a status and content-type expectation across endpoints with a response specification.
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 and validate statusCode(200)
// TODO: Print "GET /users - Status 200 OK"
}
}