{"id":54012,"date":"2021-12-13T12:53:08","date_gmt":"2021-12-13T07:23:08","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=54012"},"modified":"2022-01-06T18:00:23","modified_gmt":"2022-01-06T12:30:23","slug":"rest-assured-framework","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/rest-assured-framework\/","title":{"rendered":"Rest Assured Framework"},"content":{"rendered":"<h4><b>What is API and API Testing?<\/b><\/h4>\n<ul>\n<li>API<br \/>\n<span style=\"font-weight: 400;\">is abbreviated as Application Programming Interface.\u00a0<\/span><span style=\"font-weight: 400;\">It is a computing interface that has a feature of transmitting and exchanging data between two separate software systems. While using applications like Facebook, Google, Twitter, Weather Forecast, we avail information via APIs. APIs are practiced to give access to data from outside the firewall.<\/span><\/li>\n<li>API Testing<br \/>\n<span style=\"font-weight: 400;\"> is a type of software testing technique that is used to test the APIs directly \u2014 from its functionality to its performance, scalability, reliability, and security. API Testing focuses on receiving an exactly appropriate request and response in an application program.<\/span><\/li>\n<\/ul>\n<h4><b>REST vs. SOAP<\/b><\/h4>\n<ul>\n<li><span style=\"font-weight: 400;\">SOAP<\/span><br \/>\n<span style=\"font-weight: 400;\">The acronym for &#8220;Simple Object Access Protocol&#8221;, SOAP uses services interfaces to uncover the business logic. This protocol explains design to be strictly followed and needs much more bandwidth and resources than REST. It defines its own security and allows XML data format only. Generally, SOAP is less preferable than REST.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">REST<\/span><br \/>\n<span style=\"font-weight: 400;\">The acronym for &#8220;Representational State Transfer&#8221;, REST can make use of SOAP because it is a concept and can avail any protocol like HTTP, SOAP, etc. It aims to exhibit business logic and does not define too many standards like SOAP. REST requires less bandwidth and resources as compared to SOAP and it enables different data formats such as Plain text, HTML, XML, JSON ,etc. This is why REST is more popular than SOAP.<\/span><\/li>\n<\/ul>\n<h4><b>REST Assured<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">REST Assured is a type of library of JAVA used to test RESTful APIs. It is extensively utilized to test JSON as well as XML- based web applications. Moreover, it aids all methods including GET, PUT, POST, PATCH, and DELETE. Prerequisites needed are Java, Maven, TestNG, and IDE (IntelliJ, Eclipse, etc.)<\/span> <span style=\"font-weight: 400;\">There are four types of HTTP methods &#8211; GET, PUT, POST, and DELETE. These requests can be mapped to CRUD operations as follows:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">GET retrieves the depiction of the resource at an identified URI. GET should have no leftover reaction on the server<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">PUT updates a resource at an identified URI. PUT can also be utilized to generate a new resource at a specified URI if the server permits clients to specify new URIs<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">POST creates a new resource. The server allocates the URI for the new object and returns the same URI enclosed with the response message<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">DELETE is used to delete a resource at an identified URI<\/span><\/li>\n<\/ul>\n<h4><b>SYNTAX<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">The syntax of Rest Assured.io is given below. It follows Behavior Driven Development (BDD). BDD is an approach that defines the behavior of a feature characteristic through instances in plain text.<\/span> <img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-54368\" src=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-1.png\" alt=\"\" width=\"860\" height=\"231\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-1.png 860w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-1-300x81.png 300w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-1-768x206.png 768w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-1-624x168.png 624w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/p>\n<h4><b>Code Description<\/b><\/h4>\n<ul>\n<li>Given()<br \/>\n<span style=\"font-weight: 400;\">\u2018Given\u2019 is a keyword that allows you to set a background. You pass the request headers, body, query, path parameters, and cookies. This is elective if these items are not required in the request.<\/span><\/li>\n<li>When()<br \/>\n<span style=\"font-weight: 400;\">\u2018When\u2019 is a keyword that tells you the scenario. For example, when a user gets a request like get or post or put something or do something else.<\/span><\/li>\n<li>Method()<br \/>\n<span style=\"font-weight: 400;\">Replace this with any of the CRUD operations (get\/post\/put\/delete)<\/span><\/li>\n<li>Then()<br \/>\n<span style=\"font-weight: 400;\">It includes the conditions like assert and matcher\u00a0<\/span><\/li>\n<\/ul>\n<h4><b>Advantages<\/b><\/h4>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">REST is open source<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It authenticates &amp; justifies the response of the requests like &#8220;get&#8221;, &#8220;put&#8221;, &#8220;patch&#8221;, &#8220;delete&#8221;<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It provides a Given\/When\/Then test annotation, which immediately makes your tests simple enough to understand<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It supports CI\/CD integration<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It can be used with any customized\/open-source reporting tool<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It can be easily integrated with JUnit and TestNG frameworks<\/span><\/li>\n<\/ol>\n<h4><b>Disadvantages<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><\/h4>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It requires prerequisite knowledge of the Java programming language<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It does not support SOAP APIs explicitly<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It has an inbuilt reporting feature<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">REST Assured is not purely thread-safe<\/span><\/li>\n<\/ol>\n<h4><b>Dependency<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">In general, dependency is an archive \u2014 JAR, ZIP , etc collected in the pom.xml file, inside of a &lt;dependencies&gt; tag<\/span><br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-54370\" src=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-2.png\" alt=\"\" width=\"860\" height=\"249\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-2.png 860w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-2-300x87.png 300w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-2-768x222.png 768w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-2-624x181.png 624w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/p>\n<h4><b>Creating a Get Request<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">The HTTP GET request is utilized to collect a resource from a server. <\/span><span style=\"font-weight: 400;\">The following example uses the get() method from the REST assured library.<\/span><br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-54371\" src=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-3.png\" alt=\"\" width=\"860\" height=\"372\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-3.png 860w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-3-300x130.png 300w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-3-768x332.png 768w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-3-624x270.png 624w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/> <span style=\"font-weight: 400;\">The above code will result in the same response which was received when opening the same URL on a browser. This is how the response will be displayed in (Integrated Development Environment) IDE console window.<\/span><\/p>\n<h4><b>Parametrizing our Test<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">It implements the concept of Data-Driven Testing. RESTful API follows two types of parameters.<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Query Parameter<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Path Parameter<\/span><\/li>\n<\/ol>\n<ul>\n<li><b>Query Parameter<\/b><br \/>\n<span style=\"font-weight: 400;\"> These types of parameters are appended at the end of the RESTful API endpoint.<br \/>\n<\/span><\/li>\n<li><b>Path Parameter<\/b><br \/>\n<span style=\"font-weight: 400;\"> These are the paths of the API endpoint. <\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">REST Assured can control both types of parameters. Let us look at an elaborated sample script for this.<\/span> <b>For Query Parameter<\/b> <img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-54372\" src=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-4.png\" alt=\"\" width=\"860\" height=\"372\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-4.png 860w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-4-300x130.png 300w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-4-768x332.png 768w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-4-624x270.png 624w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/> <b>For Path Parameter<\/b> <img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-54373\" src=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-5.png\" alt=\"\" width=\"860\" height=\"352\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-5.png 860w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-5-300x123.png 300w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-5-768x314.png 768w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/8-5-624x255.png 624w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/> <span style=\"font-weight: 400;\">Alternatively the param(), path parameters are determined using the pathParam() method.<\/span><\/p>\n<h4><b>Other features of REST Assured<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">REST Assured provides some additional features as well which are as follows &#8211;\u00a0<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It can de-serialize POJOs. It permits you to serialize the properties and values related to a Java object instance directly into a JSON or an XML document, which can then be sent to a RESTful API using the POST method<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Registering requests and responses. This can be largely useful when you need to examine API responses to create the applicable checks, or when you want to confirm that the request you are sending to an API is correct<\/span><\/li>\n<\/ul>\n<h4><b>Conclusion<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Rest Assured is a very significant framework to write tests against a REST\/hypermedia API<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">With its illuminating approach and the demonstrative method names, it grants one to easily acknowledge what a sure call is supposed to do and to return. JSONPath and Matchers grow the power and articulateness<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Rest Assured is a collection of Java libraries that entitles us to automate Rest API testing<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Rest Assured is a Java-based concept<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It helps retrieves values of request and response from complicated JSON structures<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The API request can be altered with a diversity of header, query, path param, and any session or cookies to be set<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It helps set assert statements and conditions<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Rest Assured is very attainable in the condition where the response is JSON type, its methods may not work consistently if the content type is HTML or plain text<\/span><\/li>\n<\/ul>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>What is API and API Testing? API is abbreviated as Application Programming Interface.\u00a0It is a computing interface that has a feature of transmitting and exchanging data between two separate software systems. While using applications like Facebook, Google, Twitter, Weather Forecast, we avail information via APIs. APIs are practiced to give access to data from outside [&hellip;]<\/p>\n","protected":false},"author":1408,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":16},"categories":[1818,1816],"tags":[4899,4468,4898],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54012"}],"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\/1408"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=54012"}],"version-history":[{"count":12,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54012\/revisions"}],"predecessor-version":[{"id":54416,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54012\/revisions\/54416"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=54012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=54012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=54012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}