{"id":55484,"date":"2022-09-22T14:00:43","date_gmt":"2022-09-22T08:30:43","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=55484"},"modified":"2022-09-22T14:00:43","modified_gmt":"2022-09-22T08:30:43","slug":"automating-the-api-testing-using-rest-assured","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/automating-the-api-testing-using-rest-assured\/","title":{"rendered":"Automating the API testing using Rest Assured"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">In any project, we have a large number of APIs, and if we only stick to manual testing, then it will take a lot of time and resource consumption; managing this situation is also very challenging in this recurring release trend in the industry. To overcome this, it is good to start the API Automation and target to automate maximum APIs. The API automation runs very fast and gives us the execution report by which we can analyze the failure cases quickly and take the required action. The automation can be run on a daily basis by which we can quickly look at all the APIs and if anything breaks by the new code change, then we can report the issue on the same day.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>1. Introduction to the Rest Assured<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">It is a java library used to test the Restful Web Services. The full form is (REpresentational State Transfer). Using JAVA we can use this library to create a complete automation suite as per our project requirement. It supports HTTP methods like GET, POST, PUT, PATCH, and DELETE which are widely used in API testing.\u00a0<\/span><\/p>\n<h2><b>2. Getting Started with the framework creation\u00a0<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">In order to create the framework first step is we need to set up any IDE in our machine in this case I am using Eclipse the second step is to install JAVA. Now first, create a maven project and then convert it to TestNG.<\/span><\/p>\n<p><b>Steps to create a Maven Project:<\/b><span style=\"font-weight: 400;\">\u00a0In the eclipse, navigate to File &gt; New &gt; Other &gt; Select Maven Project &gt; Next &gt; Provide the Group ID and Artifact ID and create the project.<\/span><\/p>\n<p><b>Steps to convert Maven Project in TestNG: <\/b><span style=\"font-weight: 400;\">Do right-click on the maven project &gt; Select TestNG &gt; Convert TestNG.\u00a0<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-55471\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-262x300.png\" alt=\"\" width=\"262\" height=\"300\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-262x300.png 262w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0.png 300w\" sizes=\"(max-width: 262px) 100vw, 262px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Here the point of focus is that our project name is \u2018ClientManagement\u2019 and we have two folders under src\/test\/java the first one is <\/span><b>com.org.Base<\/b><span style=\"font-weight: 400;\"> and the second one is <\/span><b>com.org.Tests <\/b><span style=\"font-weight: 400;\">also I have created one config file with the name <\/span><b>config.properties<\/b><span style=\"font-weight: 400;\">. <\/span><span style=\"font-weight: 400;\">In this project, we will do the CRUD operations like adding the new user, fetching the record of any existing user in the same way update and deleting the users. <\/span><\/p>\n<h2><b>3. Adding the required dependencies in the project<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">We can add the below dependencies from here <\/span><a href=\"https:\/\/mvnrepository.com\/\"><span style=\"font-weight: 400;\">https:\/\/mvnrepository.com\/<\/span><\/a><span style=\"font-weight: 400;\"> it is always recommended to add the latest version of the dependencies. Add all the dependencies in the POM.xml file.\u00a0<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">&lt;dependency&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;groupId&gt;junit&lt;\/groupId&gt; <\/span><span style=\"font-weight: 400;\">&lt;artifactId&gt;junit&lt;\/artifactId&gt; <\/span><span style=\"font-weight: 400;\">&lt;version&gt;3.8.1&lt;\/version&gt; <\/span><span style=\"font-weight: 400;\">&lt;scope&gt;test&lt;\/scope&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;\/dependency&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;dependency&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;groupId&gt;org.testng&lt;\/groupId&gt;<\/span>\u00a0<span style=\"font-weight: 400;\">&lt;artifactId&gt;testng&lt;\/artifactId&gt; <\/span><span style=\"font-weight: 400;\">&lt;version&gt;7.0.0&lt;\/version&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;\/dependency&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;dependency&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;groupId&gt;io.rest-assured&lt;\/groupId&gt; <\/span><span style=\"font-weight: 400;\">&lt;artifactId&gt;rest-assured&lt;\/artifactId&gt; <\/span>\u00a0<span style=\"font-weight: 400;\">&lt;version&gt;4.4.0&lt;\/version&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;\/dependency&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;dependency&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;groupId&gt;io.rest-assured&lt;\/groupId&gt; <\/span><span style=\"font-weight: 400;\">&lt;artifactId&gt;json-path&lt;\/artifactId&gt; <\/span><span style=\"font-weight: 400;\">&lt;version&gt;4.1.2&lt;\/version&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;\/dependency&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;dependency&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;groupId&gt;io.rest-assured&lt;\/groupId&gt; <\/span><span style=\"font-weight: 400;\">&lt;artifactId&gt;json-schema-validator&lt;\/artifactId&gt; <\/span><span style=\"font-weight: 400;\">&lt;version&gt;3.3.0&lt;\/version&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;\/dependency&gt;<\/span><\/pre>\n<h2><b>4. Creating the required classes and adding the methods in the Base Package<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Here we will create the classes and common methods that would be used throughout the framework.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>4.1<\/strong>\u00a0In the <\/span><b>com.org.Base<\/b><span style=\"font-weight: 400;\"> package first, create a <\/span><b>Setup<\/b><span style=\"font-weight: 400;\"> class and create a method to read the data from the property file. This\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 method takes a key name as a String and returns the respective value.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-55477\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/utlnew-300x123.png\" alt=\"\" width=\"300\" height=\"123\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/utlnew-300x123.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/utlnew-768x316.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/utlnew-624x257.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/utlnew.png 929w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><b>4.2 <\/b><span style=\"font-weight: 400;\">\u00a0<\/span><b>Prepare the config file<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Here we add the URLs and test data that will be used throughout the framework and by the utility that we have already created in the <\/span><b>Setup<\/b><span style=\"font-weight: 400;\"> class we would be fetching the values.<\/span><\/p>\n<p><b>Note<\/b><span style=\"font-weight: 400;\">: All the URLs mentioned here are dummy URLs you can add your own as per your project.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-55478\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/file-300x136.png\" alt=\"\" width=\"300\" height=\"136\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/file-300x136.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/file-624x282.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/file.png 648w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><b>4.3<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Again in the <\/span><b>com.org.Base <\/b><span style=\"font-weight: 400;\">package, we create a new class <\/span><b>RestUtilities <\/b><span style=\"font-weight: 400;\">and in this class, we will create some common utilities that will be used in the framework. Here I am creating two methods the first is to add the new user to the system and the second is to get the detail of any existing user.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In the <\/span><b>RestUtilities <\/b><span style=\"font-weight: 400;\">class first, I create the object of the <\/span><b>Setup <\/b><span style=\"font-weight: 400;\">class so that we can call the methods declared in that class.\u00a0<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-55479\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-1-300x130.png\" alt=\"\" width=\"300\" height=\"130\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-1-300x130.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-1-768x333.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-1-624x271.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-1.png 987w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55480\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-2-300x90.png\" alt=\"\" width=\"304\" height=\"91\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-2-300x90.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-2-768x230.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-2-624x187.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-2.png 897w\" sizes=\"(max-width: 304px) 100vw, 304px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Both methods return the response code based on which we will write the assertion and validate the API. Likewise, we can create other methods to update the user\u2019s details and delete the user from the system.\u00a0<\/span><\/p>\n<h2><b>5. Writing the actual test script based on the requirement\u00a0<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">In the <\/span><b>com.org.test <\/b><span style=\"font-weight: 400;\">package first, we create a class <\/span><b>TestCase_UserManagement_Operations <\/b><span style=\"font-weight: 400;\">here we can write all our test cases here I am writing a sample test case to verify that a new user can be added to the system through API, for this, I will call the Add new user method <\/span><span style=\"font-weight: 400;\">declared in the <\/span><b>RestUtilities <\/b><span style=\"font-weight: 400;\">class and based on the response code I will write the assertion, in this case, the if the response code in 201 then test case will be passed.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><b>Note<\/b>: We have to use the @Test annotation from the TestNG before each test case this will help at the time of the execution.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-55481\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-3-300x103.png\" alt=\"\" width=\"300\" height=\"103\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-3-300x103.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-3-768x263.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-3-624x214.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-3.png 777w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<h2><b>6. Configuring the execution file\u00a0<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">For the test case execution here we will use the <\/span><b>TestNG.xml <\/b><span style=\"font-weight: 400;\">file, in this, we can manage all our execution control. This is the same file that got generated when we converted our project into TestNG in the initial steps.<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-55482\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-4-300x111.png\" alt=\"\" width=\"300\" height=\"111\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-4-300x111.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-4-624x232.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-4.png 630w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><br \/>\n<\/span><\/p>\n<p>We are doing the execution based on the classes, in the project all the test cases of the class<br \/>\nTestCase_UserManagement_Operations will be executed as we have passed the same class name under the &lt;class&gt; tag in the testNG.xml.<\/p>\n<p><span style=\"font-weight: 400;\">Now we can manually run the complete suite or we can schedule an automated job from any CICD tool.<\/span><\/p>\n<h2><b>7. Fetching the data from the response body<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">In the above example, we are validating the API based on the response code but in real-time we are also required to fetch the response data and its validation. We can use the below code the fetch the data from the response and then write the assertion as per the need.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-55483\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-5-300x133.png\" alt=\"\" width=\"300\" height=\"133\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-5-300x133.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-5-624x277.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/pasted-image-0-5.png 759w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">In the above code, we are fetching specific user details using user_id, now using the <\/span><b>JsonPath<\/b><span style=\"font-weight: 400;\"> we fetch the first_name from the response and write the assertion.\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>In any project, we have a large number of APIs, and if we only stick to manual testing, then it will take a lot of time and resource consumption; managing this situation is also very challenging in this recurring release trend in the industry. To overcome this, it is good to start the API Automation [&hellip;]<\/p>\n","protected":false},"author":1404,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":65},"categories":[1816],"tags":[5018,5017],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55484"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1404"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=55484"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55484\/revisions"}],"predecessor-version":[{"id":55490,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55484\/revisions\/55490"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=55484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=55484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=55484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}