Make Selenium Easy

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


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.

Required Dependencies

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

Interface ITestContext

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.

How to use ITestContext?

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.

Example Program


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.

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

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

by Amod Mahajan time to read: 4 min
%d bloggers like this: