Skip to content
CodeKruPro

Your First REST Assured Test

1/62

Your First REST Assured Test

Welcome to REST Assured! It is the most popular Java library for testing REST APIs.

In this first assignment, you will:

  1. Set the base URI with RestAssured.baseURI
  2. Make a GET request using RestAssured.get()
  3. Read the HTTP status code from the response

Task:

  1. Make a GET request to /users
  2. Print the status code in the format shown below

Expected output:

Status Code: 200

About this lesson

REST Assured GET request: send your first call and read the status code

Learn REST AssuredBeginnerGetting Started

How it works

REST Assured is a Java library for driving HTTP APIs from test code. There is no browser and no page to render: the library builds an HTTP request, sends it through Apache HttpClient, waits for the reply and wraps it in an io.restassured.response.Response. That object carries the three things every API check starts from: the status line, the response headers and the body.

The static field RestAssured.baseURI holds the scheme and host shared by every request, so a call only names the path that follows it. When it is never set, requests go to http://localhost on port 8080 (the constants DEFAULT_URI and DEFAULT_PORT), which is why a program that never sets it fails with java.net.ConnectException: Connection refused instead of reaching the practice API.

The status code is the server's one-number summary of what happened. getStatusCode() and its shorter twin statusCode() both return it as an int, while getStatusLine() gives the whole line, protocol and reason phrase included, such as HTTP/1.1 200 OK.

About the assignment

The starter already sets RestAssured.baseURI to the practice API, so your code only deals with paths. You send a single GET to the user collection and report which status code came back, the first fact any API test confirms.

What you will do

  1. Call the static RestAssured.get() method with the /users path.
  2. Keep what it returns in a variable of type Response, the import is already in the starter.
  3. Ask that response for its status code with getStatusCode() or statusCode().
  4. Print the number after the label Status Code: with one space between them.

Common mistakes

  • Leaving the scheme off baseURI

    Writing the host without https:// does not quietly fall back to a default. As soon as the request is built, REST Assured throws IllegalStateException: Target host is null, because it cannot tell a host from a path without the scheme.

  • Treating a 4xx or 5xx reply as an exception

    REST Assured hands back a 404 or 500 response like any other, no exception involved. If your code prints or uses the body without looking at the status, a broken endpoint looks like a success.

  • Forgetting that baseURI is global

    RestAssured.baseURI is a static field, so every request in the same JVM shares it. One JUnit test that points it at another server silently changes the target of every test that runs after it.

In a real test suite

In a JUnit 5 class the base URI is normally set once in a @BeforeAll method, or moved into a RequestSpecification built with RequestSpecBuilder, so each @Test method contains only its request and its checks. A status check on a list endpoint like this one is the usual smoke test that the API is reachable before the deeper checks run.

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;

public class Main {
    public static void main(String[] args) {
        RestAssured.baseURI = "https://testkru.com/api";

        // TODO: Make a GET request to "/users" and store in a Response variable
        // TODO: Print the status code in format: "Status Code: <code>"
    }
}