Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 64 – How to pass value from one API to Another API using TestNG – ITestContext

Posted on 03/21/2025 By admin

As a part of the End to End REST Assured Tutorial, in this post, we will learn to pass data from one API to another API.

Data is key in API testing and we may require to pass the output of one API as an input to another API. For example:- We get a booking id from Create Booking API. If we need to retrieve, update, partial update, or delete booking then we must need to pass the booking id to these APIs.

We will use TestNG as a testing framework so that we can easily share data among test methods.

I have already covered Sharing Data Among Test Methods In TestNG Using ITestContext. In this post, I will explain the same concept with respect to Rest Assured.

I have used Rest Assured and TestNG of the below versions:-

  org.testng testng 7.3.0 test

  io.rest-assured rest-assured 4.3.3 test

As per TestNG Javadoc, ITestContext is an interface that defines a test context that contains all the information for a given test run. An instance of this context is passed to the test listeners so they can query information about their environment.

ITestContext is a powerful interface that provides many useful methods. In this post, we will see two important methods setAttribute​(java.lang.String name, java.lang.Object value) and getAttribute​(java.lang.String name) provided by ITestContext interface.

setAttribute(atttributeName, attributeValue) – Set a custom attribute. It is similar to adding an element in a Map as key-value pair. Kindly pay attention here that attribute value can be of any type. This is the reason this method accepts Object type as a value.

getAttribute(attributeName) – Get the value of given attribute name. Remember return type is an Object.

ITestContext interface extends IAttributes interface. Instead of going theoretically, let’s learn it using examples. It will make more sense to you.

ITestContext reference is created once and can be used with the @Test annotated method by just passing it as a parameter. Whatever data you would like to use in other tests you just need to store them in Test Context using setAttribute() method. To retrieve stored data use getAttribute() method.

We will use Restful – Booker APIs for demo purposes. I have already covered many examples of these APIs in my RestAssured series.

package SharingData;

import org.testng.ITestContext;
import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;

public class ShareDataUsingITestContext {
        
        
        @Test
        public void createBooking(ITestContext context)
        {
                int bookingId = RestAssured
                .given()
                        .log()
                        .all()
                        .baseUri("https://restful-booker.herokuapp.com/")
                        .basePath("booking")
                        .contentType(ContentType.JSON)
                        .body("{\r\n" + 
                                "    \"firstname\" : \"Jim\",\r\n" + 
                                "    \"lastname\" : \"Brown\",\r\n" + 
                                "    \"totalprice\" : 111,\r\n" + 
                                "    \"depositpaid\" : true,\r\n" + 
                                "    \"bookingdates\" : {\r\n" + 
                                "        \"checkin\" : \"2018-01-01\",\r\n" + 
                                "        \"checkout\" : \"2019-01-01\"\r\n" + 
                                "    },\r\n" + 
                                "    \"additionalneeds\" : \"Breakfast\"\r\n" + 
                                "}")
                .when()
                        .post()
                .then()
                        .log()
                        .all()
                        .extract()
                        .jsonPath()
                        .get("bookingid");
                // Storing data in a context to use for other tests
                context.setAttribute("bookingId", bookingId);
        }
        
        @Test
        public void updateBooking(ITestContext context)
        {
                // Retrieving required data from context
                int bookingId = (int) context.getAttribute("bookingId");
                RestAssured
                .given()
                        .log()
                        .all()
                        .baseUri("https://restful-booker.herokuapp.com/")
                        .basePath("booking/"+bookingId)
                        .header("Authorization","Basic YWRtaW46cGFzc3dvcmQxMjM=")
                        .contentType(ContentType.JSON)
                        .body("{\r\n" + 
                                "    \"firstname\" : \"Amod\",\r\n" + 
                                "    \"lastname\" : \"Mahajan\",\r\n" + 
                                "    \"totalprice\" : 222,\r\n" + 
                                "    \"depositpaid\" : true,\r\n" + 
                                "    \"bookingdates\" : {\r\n" + 
                                "        \"checkin\" : \"2022-01-01\",\r\n" + 
                                "        \"checkout\" : \"2022-01-01\"\r\n" + 
                                "    },\r\n" + 
                                "    \"additionalneeds\" : \"Breakfast\"\r\n" + 
                                "}")
                .when()
                        .put()
                .then()
                        .log()
                        .all();
                        
        }
        
        

}
[RemoteTestNG] detected TestNG version 7.0.1
Request method: POST
Request URI:    https://restful-booker.herokuapp.com/booking
Proxy: 
Request params: 
Query params:   
Form params:    
Path params:    
Headers: Accept=*/* Content-Type=application/json; charset=UTF-8
Cookies: 
Multiparts: 
Body:
{ "firstname": "Jim", "lastname": "Brown", "totalprice": 111, "depositpaid": true, "bookingdates": { "checkin": "2018-01-01", "checkout": "2019-01-01" }, "additionalneeds": "Breakfast"
}
HTTP/1.1 200 OK
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 195
Etag: W/"c3-mr+ToVU3dK7bNqxVpsnhD1SC7cM"
Date: Wed, 27 Jan 2021 06:23:23 GMT
Via: 1.1 vegur { "bookingid": 11, "booking": { "firstname": "Jim", "lastname": "Brown", "totalprice": 111, "depositpaid": true, "bookingdates": { "checkin": "2018-01-01", "checkout": "2019-01-01" }, "additionalneeds": "Breakfast" }
}
Request method: PUT
Request URI:    https://restful-booker.herokuapp.com/booking/11
Proxy: 
Request params: 
Query params:   
Form params:    
Path params:    
Headers: Authorization=Basic YWRtaW46cGFzc3dvcmQxMjM= Accept=*/* Content-Type=application/json; charset=UTF-8
Cookies: 
Multiparts: 
Body:
{ "firstname": "Amod", "lastname": "Mahajan", "totalprice": 222, "depositpaid": true, "bookingdates": { "checkin": "2022-01-01", "checkout": "2022-01-01" }, "additionalneeds": "Breakfast"
}
HTTP/1.1 200 OK
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 171
Etag: W/"ab-+iqLT0fOvVL3GfV0ed6NlH849m8"
Date: Wed, 27 Jan 2021 06:23:25 GMT
Via: 1.1 vegur { "firstname": "Amod", "lastname": "Mahajan", "totalprice": 222, "depositpaid": true, "bookingdates": { "checkin": "2022-01-01", "checkout": "2022-01-01" }, "additionalneeds": "Breakfast"
}
PASSED: createBooking(org.testng.TestRunner@1990a65e)
PASSED: updateBooking(org.testng.TestRunner@1990a65e) =============================================== Default test Tests run: 2, Failures: 0, Skips: 0
=============================================== ===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

You can download/clone the above sample project from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Git Tutorial 2 – What is GIT and Its Advantages?
Next Post: Java Program To Find Closest Value Of A Given Number in Unsorted Array

Related Posts

#5. OAuth 2.0 Flow – What Is A Refresh Token? Uncategorized
Bug Life Cycle – Make Selenium Easy Uncategorized
TestNG Tutorials 36: Can a Test Method Return a Value in TestNG? – Make Selenium Easy Uncategorized
TestNG Tutorials 26: Understand Usage of alwaysRun Attribute With Test Method of TestNG Uncategorized
SELENIUM INTERVIEW QUESTION 6 – All Ways to Exclude/Ignore/Disable a Test Method in TestNG Uncategorized
regInCLasses – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark