Writing Your First Test
Now that your environment is set up and you understand RESTful APIs, let's write your first REST Assured test! We'll start with a simple GET request to fetch data from an API.
What is a GET Request?
A GET request is used to retrieve information from a server. For example:
- Getting user details.
- Fetching a list of products.
- Viewing order history.
What Will We Do in This Test?
We will:
- Send a GET request to a Book Library API.
- Check if the response status is 200 (OK).
- Print the response to the console.
We will use the free Book Library API, which provides fake data for testing.
API Endpoint:
http://192.xxx.xx.xxx:8080/books/20250401
This will return details of the book with ISBN 20250401.
Step 1: Create a New Java Class
In your Maven project, go to the src/test/java folderand create a new class called:
FirstRestAssuredTest.java
Step 2: Write the GET Request Test
Add the following code to your class:
import io.restassured.RestAssured; import io.restassured.response.Response; import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; public class FirstRestAssuredTest { public static void main(String[] args) { // Set the base URL for the API RestAssured.baseURI = "http://192.xxx.xx.xxx:8080"; // Send a GET request and store the response Response response = given() .when() .get("/books/20250401"); // Print the response body System.out.println("Response: " + response.getBody().asString()); // Validate the response status code is 200 (OK) response.then().statusCode(200); System.out.println("Test Passed: Status Code is 200"); } }
Step 3: Run the Test
- Right-click on the FirstRestAssuredTest class.
- Select Run 'FirstRestAssuredTest'.
If everything is set up correctly, you should see output like this:
Response: {
"price": 100.0,
"isbn": "20250401",
"name": "Book 1",
"publisher": "ABC publisher",
"language": "English"
}
Test Passed: Status Code is 200
Congratulations! You just wrote and ran your first REST Assured test.
Understanding the Code
Let's break down the code step by step:
1. Set the Base URL
RestAssured.baseURI = "http://192.xxx.xx.xxx:8080";
This tells REST Assured which API to interact with.
2. Send the GET Request
Response response = given().when().get("/books/20250401");
- given() – Prepares the request (you can add headers, query parameters, etc.).
- when() – Tells REST Assured to send the request.
- get("/books/20250401") – Sends a GET request to the /books/20250401 endpoint.
3. Print the Response
System.out.println("Response: " + response.getBody().asString());
This prints the response body (the data returned by the API).
4. Validate the Status Code
response.then().statusCode(200);
This checks if the status code is 200 (OK). If not, the test will fail.
Step 4: Add More Validations
Let's improve the test by checking specific fields in the response.
Update your test with these extra assertions:
response.then() .statusCode(200) // Check status code .body("isbn", equalTo(20250401)) // Check Book ISBN .body("name", equalTo("Book 1"))// Check Book name .body("publisher", containsString("ABC")); // Check part of the Publisher
If the validation fails, REST Assured will show an error.
Step 5: Handling Errors Gracefully
If the API is incorrect or unavailable, REST Assured will fail the test.
For example, testing an invalid book:
Response invalidBook = given().when().get("/books/123"); invalidBook.then().statusCode(404); // book not found