Getting Started with REST Assured: A Beginner-Friendly Guide
What is REST Assured?
REST Assured is a Java library widely used for API Automation Testing. It simplifies the process of validating REST APIs by providing an easy-to-read and expressive syntax.
Using REST Assured, testers can easily test:
- REST APIs
- Web Services
- HTTP Requests and Responses
- API Response Validations
- Authentication and Authorization Flows
REST Assured supports all major HTTP methods:
- GET — Retrieve data from a server
- POST — Create new resources
- PUT — Update existing resources
- DELETE — Remove resources
- PATCH — Partially update resources
Its syntax is inspired by BDD (Behavior Driven Development), making test cases highly readable and easy to understand.
Understanding the BDD Structure in REST Assured
REST Assured primarily follows the Given — When — Then pattern.
This structure makes API test cases self-explanatory and closely resembles natural language.
Given → Setup Request Data
The given() method is used to define all the input data required for the API request.
Typical request details configured inside given() include:
- Query Parameters
- Path Parameters
- Headers
- Cookies
- Authentication Details
- Request Body
Think of given() as the precondition or setup phase of your API test.
Example
given()
.queryParam("orderId", "12345")
.header("Content-Type", "application/json")When → Send the Request
The when() method is responsible for triggering the API request.
Inside this section, we specify:
- The HTTP Method (GET, POST, PUT, DELETE, PATCH)
- The API Endpoint or Resource URI
Think of when() as the action phase where the actual request is sent to the server.
Example
.when()
.get("/orders")Then → Validate the Response
The then() method is used for validating the API response.
Get Sonuray’s stories in your inbox
Join Medium for free to get updates from this writer.
All assertions and response validations are written here, such as:
- Status Code Validation
- Response Body Validation
- Response Header Validation
- Response Time Validation
Think of then() as the verification phase of the test.
Example
.then()
.statusCode(200)Complete Example
Let’s look at a real-world example that fetches order details from an API.
package ItemList;import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.testng.annotations.Test;import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;public class ItemListTest { @Test
public void getOrderList() { RestAssured.baseURI = "http://prod-omsagg.nykfashion-int.net/"; Response response =
given()
.queryParam("orderSource", "NYKAA_FASHION")
.queryParam("orderId", "NYK-39917576-5146577")
.queryParam("emailId", "8766203480@nykaafashion.com")
.queryParam("userId", "53302707") .when()
.get("omsApis/v2/orders") .then()
.assertThat()
.statusCode(200)
.extract()
.response();
}
}
Breaking Down the Example
Step 1: Define the Base URI
RestAssured.baseURI = "http://prod-omsagg.nykfashion-int.net/";This specifies the server against which the API request will be executed.
Step 2: Provide Request Parameters
given()
.queryParam("orderSource", "NYKAA_FASHION")
.queryParam("orderId", "NYK-39917576-5146577")These query parameters will be appended to the request URL before execution.
Step 3: Send the Request
.when()
.get("omsApis/v2/orders")This sends a GET request to the specified endpoint.
Step 4: Validate the Response
.then()
.statusCode(200)This verifies that the API successfully returned an HTTP 200 OK response.
Why Use Static Imports?
When working with REST Assured, you’ll frequently use methods such as given() and matchers such as equalTo(), containsString(), or hasItem().
To make your code cleaner and more readable, it is recommended to use static imports.
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;Benefits
- Cleaner syntax
- Better readability
- Less boilerplate code
- More expressive test cases
Without Static Import
RestAssured.given()With Static Import
given()The second version is much cleaner and aligns well with the BDD style that REST Assured promotes.
Conclusion
REST Assured is one of the most popular frameworks for API Automation Testing in the Java ecosystem. Its BDD-inspired syntax makes API tests easy to write, understand, and maintain.
By mastering the Given → When → Then structure, you can create API tests that are both readable and scalable.
To summarize:
- Given → Configure request details
- When → Send the API request
- Then → Validate the API response
If you’re starting your API automation journey with Java, REST Assured is an excellent framework to learn and use in real-world projects.



