REST Assured follows a BDD pattern: given-when-then. If you have ever described a test scenario in plain English, you already know this pattern. "Given I have a valid auth token, when I call GET /users, then I should get 200 OK." REST Assured turns that English sentence into Java code.
Let us write the simplest possible test. We will call a public API and verify the status code. That is it. Baby steps.
import io.restassured.RestAssured;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class FirstTest {
@BeforeClass
public void setup() {
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
}
@Test
public void testGetAllPosts() {
given() // Setup — nothing special needed here
.log().uri() // Print the URL being called
.when()
.get("/posts") // Action — send GET request
.then()
.statusCode(200) // Assert — status must be 200
.body("size()", equalTo(100)); // Assert — 100 posts
}
}Run this with right-click > Run in IntelliJ, or from terminal: mvn test -Dtest=FirstTest. If you see green, you just wrote your first REST Assured test.
| Part | What It Does | Real-Life Analogy |
|---|---|---|
| RestAssured.baseURI | Sets the base URL for all requests | Setting your GPS to the city — every destination starts from here |
| given() | Sets up the request — headers, params, body | Packing your bag before a trip |
| when() | Sends the HTTP request | Actually boarding the plane |
| then() | Validates the response | Checking if you arrived at the right place |
| .statusCode(200) | Asserts the HTTP status | Checking the flight landed, not crashed |
| .body("size()", equalTo(100)) | Asserts response body content | Checking you got all your luggage |
REST Assured relies on static imports. Without them, you would have to write RestAssured.given() instead of just given(). These two imports go at the top of every test class. Memorize them.
// These two lines go at the top of EVERY REST Assured test class
import static io.restassured.RestAssured.*; // given(), when(), then()
import static org.hamcrest.Matchers.*; // equalTo(), hasSize(), etc.If IntelliJ shows "cannot find symbol: given" or "cannot find symbol: equalTo" — you are missing the static imports. These are the most common compilation errors for beginners. Add the two import static lines and the errors disappear.
@Test
public void testGetSinglePost() {
given()
.header("Accept", "application/json") // Set request header
.pathParam("postId", 1) // Path parameter
.when()
.get("/posts/{postId}") // URL with path param
.then()
.statusCode(200)
.body("id", equalTo(1)) // Body field check
.body("userId", equalTo(1))
.body("title", notNullValue()) // Not null check
.body("body", containsString("quia")); // Contains substring
}Key Point: given() = setup. when() = action. then() = assert. This pattern reads like English and is the foundation of every REST Assured test you will ever write.
Q: Explain the given-when-then pattern in REST Assured.
A: REST Assured uses a BDD-style fluent API. given() sets up the request — headers, query params, path params, request body, content type, authentication. when() specifies the HTTP method and endpoint — get(), post(), put(), delete(). then() validates the response — status code, response body fields, headers, cookies. The code reads almost like English: given a JSON content type and auth token, when I POST to /users, then I expect status 201 and body containing the user ID. This makes tests readable even for non-developers.
Key Point: given() sets up the request. when() sends it. then() validates the response. Always add the two static imports for RestAssured.* and Matchers.*.