REST Assured Mini Project 1 — Automating reqres.in mock API with REST Assured, TestNG and java

Udara Kaushalya Kammanankada
4 min readSep 7, 2022

Prerequisites and tools required

  • IntelliJ/Netbeans with java installed.
    Note: I’m using IntelliJ as my IDE for this project

What is REST Assured?

Rest Assured enables you to test REST APIs using java libraries and integrates well with Maven. It has very efficient matching techniques, so asserting your expected results is also pretty straight forward. Rest Assured has methods to fetch data from almost every part of the request and response no matter how complex the JSON structures are.

Source: guru99.com

What we are going to do in this mini project is first retrieve the record using GET method, then update it using PATCH method and then verify the updated record again with GET method with assertions.

Step 1

  • In Intellij create a new project -> choose Maven archtype and create a maven project

Step 2

Now let’s add Selenium and TestNG dependencies to pom file
- Navigate to https://mvnrepository.com/
- Search REST Assured and click on the latest release

Copy the code in maven tab, navigate back to Intellij and in pom.xml file paste the copied code in between <dependencies> copied code </dependencies>
if your pom.xml doesn’t show dependencies tags you need to create it.

Search as TestNG and as mentioned in the first step copy the code to pom.xml file. Your Pom.xml file should look as below after the adding’s.

Copy the code in maven tab, navigate back to Intellij and in pom.xml file paste the copied code in between <dependencies> copied code </dependencies>
if your pom.xml doesn’t show dependencies tags you need to create it.

Search as TestNG and as mentioned in the first step, copy the code to pom.xml file. Your Pom.xml file should look as below after the adding’s.

Step 3

Now, let’s create a java class file and let’s start to do the coding. Since we
created a maven project it will automatically download the required JAR files.

  • Now let’s create a class file. Before creating a class if its doesn't show src/test folder, right click on project ,New -> directory and add it

Mock API : https://reqres.in

Now let’s go through the code. Inside the main method will do the coding.First we need to give the main URL of the API that we need to work on.

RestAssured.baseURI = "https://reqres.in";

Now we need to automate the GET request using REST.

URL-https://reqres.in/api/users/2
Method- GET

Code:

String response1 =   given().log().all().when().get("/api/users/2").then().assertThat().statusCode(200).extract().response().asString();
System.out.println(response1);

REST Assured use given when then pattern in coding. usually in given specifying query parameters, headers and body etc. Inside when specifying the method and inside then specifying the status codes that we needed.

Next, need to update the user details using patch method

String response2 =   given().log().all().body(BodyJson.patch()).when().patch("/api/users/2").then().assertThat().statusCode(200).extract().response().asString();
System.out.println(response2);

I’ve created a separate class to store the body content as below

public class BodyJson {



public static String patch()
{
return "{\n" +
" \"first_name\": \"john\",\n" +
" \"last_name\": \"doe\"\n" +
"}";
}

}

Now we need to use GET method again to verify that the updated user information presented in the API.

String response3 =   given().log().all().when().get("/api/users/2").then().assertThat().statusCode(200).extract().response().asString();
System.out.println(response3);

Now since this a mock API even the user name is updated in the previous patch method it doesn't reflect since this a mock API. so we use this is to our advantage is to verify that the updated user name and current user name and return an error if it’s not matched.

Codes for the verification part:

JsonPath jsl = new JsonPath(response3);
String firstname = jsl.getString("data.first_name");
System.out.println(firstname);
String updatedfirstname = "john";
Assert.assertEquals(firstname,updatedfirstname);

Jsonpath is used to extract certain parameters from JSON response. In here first we will retrieve firstname in the response to a string variable called firstname. Created another variable as updatedfirstname for the updated name as per PATCH method which is john. Then compare the updated first name and first name.

Since this a mock API and patch method is not properly updated with the values we have sent, program will return an error saying the names didn’t match.

Full code

import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import org.testng.Assert;

import static io.restassured.RestAssured.given;

public class API {

public static void main(String[] args) {
RestAssured.baseURI = "https://reqres.in";


String response1 = given().log().all().when().get("/api/users/2").then().assertThat().statusCode(200).extract().response().asString();
System.out.println(response1);

String response2 = given().log().all().body(BodyJson.patch()).when().patch("/api/users/2").then().assertThat().statusCode(200).extract().response().asString();
System.out.println(response2);


String response3 = given().log().all().when().get("/api/users/2").then().assertThat().statusCode(200).extract().response().asString();
System.out.println(response3);

JsonPath jsl = new JsonPath(response3);
String firstname = jsl.getString("data.first_name");
System.out.println(firstname);
String updatedfirstname = "john";
Assert.assertEquals(firstname,updatedfirstname);

}



}

Github Repo link : https://github.com/Udarakammanankada/reqres-sample-api

Hope you guys enjoyed the article!

--

--

Udara Kaushalya Kammanankada

Senior QA Engineer who passionate about selenium automation and Rest Assured API automation projects.