How To Pass Data Using Put To Rest Service In Java
Introduction
Every bit a part of theStop to End REST Assured Tutorial, in this mail service, nosotros will learn to pass data from one API to another API.
Information 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 go a booking id from Create Booking API. If nosotros need to retrieve, update, partial update, or delete booking then we must demand to laissez passer the booking id to these APIs.
We will use TestNG as a testing framework so that nosotros can easily share information among test methods.
I accept already covered Sharing Data Amidst Test Methods In TestNG Using ITestContext. In this postal service, I will explicate the aforementioned concept with respect to Residue Assured.
Required Dependencies
I have used Remainder Bodacious and TestNG of the below versions:-
< ! -- https : //mvnrepository.com/artifact/org.testng/testng --> < dependency > < groupId > org . testng < / groupId > < artifactId > testng < / artifactId > < version > vii.iii.0 < / version > < scope > test < / scope > < / dependency > |
< ! -- https : //mvnrepository.com/artifact/io.rest-assured/rest-bodacious --> < dependency > < groupId > io . residue - assured < / groupId > < artifactId > rest - assured < / artifactId > < version > four.three.3 < / version > < telescopic > examination < / telescopic > < / dependency > |
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 and then they tin can query information about their surroundings.
ITestContext is a powerful interface that provides many useful methods. In this mail service, we will see two important methods setAttribute(java.lang.Cord name, java.lang.Object value) and getAttribute(java.lang.String name) provided by ITestContext interface.
setAttribute()
setAttribute(atttributeName, attributeValue) – Ready a custom aspect. Information technology is similar to calculation an element in a Map every bit 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()
getAttribute(attributeName) – Get the value of given attribute name. Remember render type is an Object.
How to use ITestContext?
ITestContext interface extends IAttributes interface. Instead of going theoretically, permit's learn information technology using examples. It volition brand more sense to y'all.
ITestContext reference is created one time and can be used with the @Exam annotated method by only passing it as a parameter. Whatsoever data you would like to apply in other tests you simply need to store them in Test Context using setAttribute() method. To recollect stored information employ getAttribute() method.
Nosotros will use Restful – Booker APIs for demo purposes. I have already covered many examples of these APIs in my RestAssured serial.
Instance Program
1 ii three iv v six 7 8 9 ten eleven 12 13 14 fifteen 16 17 18 19 20 21 22 23 24 25 26 27 28 29 xxx 31 32 33 34 35 36 37 38 39 xl 41 42 43 44 45 46 47 48 49 l 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | package SharingData ; import org . testng . ITestContext ; import org . testng . annotations . Exam ; import io . restassured . RestAssured ; import io . restassured . http . ContentType ; public grade 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\northward" + " \"totalprice\" : 111,\r\due north" + " \"depositpaid\" : true,\r\n" + " \"bookingdates\" : {\r\n" + " \"checkin\" : \"2018-01-01\",\r\n" + " \"checkout\" : \"2019-01-01\"\r\north" + " },\r\n" + " \"additionalneeds\" : \"Breakfast\"\r\n" + "}" ) . when ( ) . post ( ) . so ( ) . log ( ) . all ( ) . excerpt ( ) . jsonPath ( ) . get ( "bookingid" ) ; // Storing data in a context to use for other tests context . setAttribute ( "bookingId" , bookingId ) ; } @ Examination 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 ( "Say-so" , "Basic YWRtaW46cGFzc3dvcmQxMjM=" ) . contentType ( ContentType . JSON ) . trunk ( "{\r\due north" + " \"firstname\" : \"Amod\",\r\northward" + " \"lastname\" : \"Mahajan\",\r\northward" + " \"totalprice\" : 222,\r\n" + " \"depositpaid\" : truthful,\r\due north" + " \"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 ( ) ; } } |
Output
1 2 3 four five 6 7 8 9 10 11 12 13 14 fifteen xvi 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 twoscore 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 ninety 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | [ RemoteTestNG ] detected TestNG version vii.0.1 Request method : POST Request URI : https : //restful-booker.herokuapp.com/booking Proxy : < none > Asking params : < none > Query params : < none > Form params : < none > Path params : < none > Headers : Accept = * /* Content-Type=awarding/json; charset=UTF-eight Cookies: <none> Multiparts: <none> Body: { "firstname": "Jim", "lastname": "Dark-brown", "totalprice": 111, "depositpaid": truthful, "bookingdates": { "checkin": "2018-01-01", "checkout": "2019-01-01" }, "additionalneeds": "Breakfast" } HTTP/i.1 200 OK Server: Cowboy Connection: keep-alive X-Powered-By: Express Content-Blazon: awarding/json; charset=utf-8 Content-Length: 195 Etag: W/"c3-mr+ToVU3dK7bNqxVpsnhD1SC7cM" Date: Wed, 27 Jan 2021 06:23:23 GMT Via: one.i vegur { "bookingid": 11, "booking": { "firstname": "Jim", "lastname": "Dark-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/xi Proxy: <none> Request params: <none> Query params: <none> Class params: <none> Path params: <none> Headers: Authority=Basic YWRtaW46cGFzc3dvcmQxMjM= Have=*/ * Content - Blazon = application / json ; charset = UTF - eight Cookies : < none > Multiparts : < none > Body : { "firstname" : "Amod" , "lastname" : "Mahajan" , "totalprice" : 222 , "depositpaid" : true , "bookingdates" : { "checkin" : "2022-01-01" , "checkout" : "2022-01-01" } , "additionalneeds" : "Breakfast" } HTTP / 1.one 200 OK Server : Cowboy Connection : keep - alive X - Powered - By : Express Content - Type : application / json ; charset = utf - 8 Content - Length : 171 Etag : Westward / "ab-+iqLT0fOvVL3GfV0ed6NlH849m8" Date : Midweek , 27 Jan 2021 06 : 23 : 25 GMT Via : 1.i 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 : two , Failures : 0 , Skips : 0 === === === === === === === === === === === === === === === == === === === === === === === === === === === === === === === == Default suite Total tests run : two , Passes : 2 , Failures : 0 , Skips : 0 === === === === === === === === === === === === === === === == |
You can download/clone the above sample project from hither.
You can subscribe to my YouTube channelRetargetCommon to larn from video tutorials.
If you accept any doubt, experience free to comment below.
If yous like my posts, please similar, comment, share and subscribe.
#ThanksForReading
#HappyLearning
Find all Selenium related posts hither, all API manual and automation related posts here, and find often asked Java Programs here.
Many other topics you can navigate through the menu.
Author: Amod Mahajan
My name is Amod Mahajan and I am an IT employee with 6+ years of feel in Software testing and staying in Bengaluru. My expanse of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani every bit his tutorials are piece of cake and cover basics to advance. I accept habit of exploring concepts by deep diving. I used to brand notes. I thought of sharing my knowledge through posts and at present I am here. #KeepLearning #ShareLearning
How To Pass Data Using Put To Rest Service In Java,
Source: http://makeseleniumeasy.com/2021/01/27/rest-assured-tutorial-64-how-to-pass-value-from-one-api-to-another-api-using-testng-itestcontext/
Posted by: buellthisced.blogspot.com
0 Response to "How To Pass Data Using Put To Rest Service In Java"
Post a Comment