1. Setup
Before writing any Rest Assured test, we need to set up:
- A Java project (Maven or Gradle is preferred).
- Add the Rest Assured dependency in pom.xml or build.gradle.
- Optionally add TestNG or JUnit as the testing framework.
2. Import Required Classes
- In a Rest Assured test, you typically import:
- Rest Assured methods for sending requests.
- Assertions from TestNG/JUnit to validate the response.
3. Define the Base URI
- You specify the API endpoint you want to test.
- Example: If you are testing a sample API like https://reqres.in/api/users, this becomes your base URI.
4. Send a Request
You make a request to the API:
- GET request to fetch data.
- POST request to send data.
- PUT/PATCH request to update data.
- DELETE request to remove data.
The request can include headers, query parameters, or body depending on the API requirements.
5. Capture the Response
When the API responds, Rest Assured stores details such as:
- Status code (200, 201, 400, 404, etc.)
- Response body (JSON, XML, text, etc.)
- Headers (content-type, server info, etc.)
- Response time
6. Validate the Response
Assertions are applied to verify if the API behaves as expected. Typical checks are:
- Status code is correct (e.g., 200 for success).
- Response body contains expected values (e.g., user name, ID).
- Headers are correct (e.g., content-type is application/json).
- Response time is within acceptable limits.
7. Organize into a Test Case
The above steps (setup → request → response → validation) are wrapped inside a test method using TestNG or JUnit. This test can then be executed like any other automated test case.
Below we are automating GET API from reqres.in
Add Maven Dependencies
<dependencies> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>5.4.0</version> <scope>test</scope> </dependency>
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.9.0</version> <!-- Latest version --> <scope>test</scope> </dependency></dependencies>
Take a simple example of API:
https://reqres.in/api/users
Rest Assured Code:
To get Status code of given API:
@Test public void test() { Response response = RestAssured.get("https://reqres.in/api/users?page=2"); System.out.println(response.statusCode()); }
To log all response on console:
@Test public void test1() { RestAssured.baseURI = "https://reqres.in/api/"; RestAssured.given().get("users?page=2").then().statusCode(200).log().all(); }
Code Explanation:
| Line | Purpose |
|---|---|
| baseURI | Sets the base URL. |
| given() | Prepares request specification. |
| when().get() | Triggers GET request. |
| then() | Begins response verification. |
| log().all() | Logs complete request/response. |
| statusCode(200) | Asserts HTTP status. |
Suggested Posts:
1. Execute Playwright Script on Chrome Browser
2. How to extract Response in Rest Assured by JSONPath
3. Test PUT API in RestAssured
4. Test DELETE API in RestAssured
5. How to Test Status Line of API by RestAssured