In the last tutorial, I have explained the Setup of the REST Assured Maven Project In Eclipse IDE. In this tutorial, I will automate a GET Request. I will verify the status code, line of Status, and content of the Response.
RestAssured is a class that consists of many static fields and methods. It supports POST, GET, PUT, DELETE, HEAD, PATCH, and OPTIONS requests and verifies the response of these requests.
Below are the steps to test a GET Request using Rest Assured:
Step 1 – Specify the base URL to the RESTful web service using RestAssured class.
RestAssured.baseURI = "http://dummy.restapiexample.com/api/v1/employees";
Step 2 – Every Request in Rest-Assured library is represented by an interface called RequestSpecification. This interface allows to modify the request, like adding headers or adding authentication details.
requestSpecification = RestAssured.given();
RequestSpecification is imported from package:
import io.restassured.specification.RequestSpecification;
Step 3 – Send the request to the server and recieve the response of the request made by REST Assured. This response contains every details returned by hitting request i.e. response body, response headers, status code, status lines, cookies etc.
response = requestSpecification.get();
Response is imported from package:
import io.restassured.response.Response;
Step 4 – To validate response like status code or value , we need to get reference of type ValidatableResponse. ValidatableResponse is an interface. A validatable response of a request made by REST Assured. ValidatableResponse is imported from package:
import io.restassured.response.ValidatableResponse;
PrettyPrint() – It print the response body if possible and return it as string. Pretty printing is possible for content-types JSON, XML and HTML.
Below is the example of creating a test in Non-BDD format where I have used ValidatableResponse for the assertion of status and status line of the Response.
public class Get_NonBDDDemo {
RequestSpecification requestSpecification;
Response response;
ValidatableResponse validatableResponse;
@Test
public void verifyStatusCode() {
RestAssured.baseURI = "http://dummy.restapiexample.com/api/v1/employees";
// Create a request specification
requestSpecification = RestAssured.given();
// Calling GET method
response = requestSpecification.get();
// Let's print response body.
String resString = response.prettyPrint();
System.out.println("Respnse Details : " + resString);
/*
* To perform validation on response, we need to get ValidatableResponse type of
* response
*/
validatableResponse = response.then();
// Get status code
validatableResponse.statusCode(200);
// Check status line is as expected
validatableResponse.statusLine("HTTP/1.1 200 OK");
}
}
If you don’t want to use ValidatableResponse for the assertion, you can use Response from io.restassured .response to get the status code and status line which are asserted using JUnit.Assert.
// Get status line
String statusLine = response.getStatusLine();
Assert.assertEquals(statusLine, "HTTP/1.1 200 OK");
// Get status code
int statusCode = response.getStatusCode();
Assert.assertEquals(statusCode, 200);
The below image shows the test result of the above test.

Below is the test implemented in BDD Format. In this test, I am asserting the data of Employee of Id 2. I have validated the name of the employee as well as the response message.
- equalTo is used for assertion, is imported from a static hamcrest package:
import static org.hamcrest.Matchers.equalTo;
2. given is a static import from package:
import static io.restassured.RestAssured.given;
public class Get_BDDDemo {
@Test
public void verifyUser() {
// Given
given()
// When
.when()
.get("http://dummy.restapiexample.com/api/v1/employee/2")
// Then
.then()
.statusCode(200).statusLine("HTTP/1.1 200 OK")
// To verify booking id at index 3
.body("data.employee_name", equalTo("Garrett Winters"))
.body("message", equalTo("Successfully! Record has been fetched."));
}
}

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!