<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Niteshsaini on Medium]]></title>
        <description><![CDATA[Stories by Niteshsaini on Medium]]></description>
        <link>https://medium.com/@niteshsaini02?source=rss-64aa8c038fa9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*62p3qx4MP6X2Zjz3</url>
            <title>Stories by Niteshsaini on Medium</title>
            <link>https://medium.com/@niteshsaini02?source=rss-64aa8c038fa9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 22:20:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@niteshsaini02/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[API Integration testing using RestAssure]]></title>
            <link>https://medium.com/@niteshsaini02/api-integration-testing-using-restassure-99a53e590e7a?source=rss-64aa8c038fa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/99a53e590e7a</guid>
            <category><![CDATA[apache-poi]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[restassured]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[data-provider]]></category>
            <dc:creator><![CDATA[Niteshsaini]]></dc:creator>
            <pubDate>Fri, 10 Jan 2020 10:17:17 GMT</pubDate>
            <atom:updated>2020-01-10T10:17:17.313Z</atom:updated>
            <content:encoded><![CDATA[<h3>Data-driven API tests with Rest Assured and TestNG</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aQKOqHvU5RMviD17POym3A.png" /></figure><h4>Combining REST Assured and TestNG</h4><p>REST Assured tests are often combined with <a href="http://testng.org/doc/index.html">TestNG</a>, and the latter offers an easy to use a mechanism to create data-driven tests through the use of the DataProvider mechanism.</p><h3>Reading data from the Excel</h3><p>We need a way to open this Excel sheet and read data from it within our test script. For this purpose, I use the Apache POI library, which allows you to read, create and edit Microsoft Office-documents using Java. The classes and methods we are going to use to read data from the Excel sheet.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0aZn0aspC6EB1AJINQWqMw.png" /></figure><h3>JSON Schema validation</h3><p>From version 2.1.0 REST Assured has support for <a href="http://json-schema.org/">JSON Schema</a> validation. For example, given the following schema located in the classpath as products-schema.json:</p><pre>{<br>  <strong>&quot;definitions&quot;</strong>: {},<br>  <strong>&quot;$schema&quot;</strong>: <strong>&quot;http://json-schema.org/draft-03/schema&quot;</strong>,<br>  <strong>&quot;type&quot;</strong>: <strong>&quot;object&quot;</strong>,<br>  <strong>&quot;title&quot;</strong>: <strong>&quot;The Root Schema&quot;</strong>,<br>  <strong>&quot;properties&quot;</strong>: {<br>    <strong>&quot;body&quot;</strong>: {<br>      <strong>&quot;type&quot;</strong>: <strong>&quot;object&quot;</strong>,<br>      <strong>&quot;title&quot;</strong>: <strong>&quot;The Body Schema&quot;</strong>,<br>      <strong>&quot;properties&quot;</strong>: {<br>        <strong>&quot;code&quot;</strong>: {<br>          <strong>&quot;type&quot;</strong>: <strong>&quot;string&quot;</strong>,<br>          <strong>&quot;title&quot;</strong>: <strong>&quot;The Code Schema&quot;</strong>,<br>          <strong>&quot;pattern&quot;</strong>: <strong>&quot;^(.*)$&quot;<br>        </strong>},<br>        <strong>&quot;data&quot;</strong>: {<br>          <strong>&quot;type&quot;</strong>: <strong>&quot;object&quot;</strong>,<br>          <strong>&quot;title&quot;</strong>: <strong>&quot;The Data Schema&quot;<br>        </strong>}<br>      }<br>    },<br>    <strong>&quot;statusCode&quot;</strong>: {<br>      <strong>&quot;type&quot;</strong>: <strong>&quot;string&quot;</strong>,<br>      <strong>&quot;title&quot;</strong>: <strong>&quot;The Statuscode Schema&quot;</strong>,<br>      <strong>&quot;pattern&quot;</strong>: <strong>&quot;^(.*)$&quot;<br>    </strong>},<br>    <strong>&quot;statusCodeValue&quot;</strong>: {<br>      <strong>&quot;type&quot;</strong>: <strong>&quot;integer&quot;</strong>,<br>      <strong>&quot;title&quot;</strong>: <strong>&quot;The Statuscodevalue Schema&quot;<br>    </strong>}<br>  }<br>}</pre><p>You can validate that a resource (/products) conforms with the schema:</p><pre>request.when().get(endpoint)<br>        .then()<br>        .assertThat()<br>        .statusCode(statusCode).and()<br>        .body(<em>matchesJsonSchemaInClasspath</em>(<strong>&quot;</strong><strong>products-schema.json&quot;</strong>));</pre><p>matchesJsonSchemaInClasspath is statically imported from io.restassured.module.jsv.JsonSchemaValidator and it&#39;s recommended to statically import all methods from this class. However, in order to use it you need to depend on the json-schema-validator module by either <a href="http://dl.bintray.com/johanhaleby/generic/json-schema-validator-4.1.2-dist.zip">downloading</a> it from the download page or add the following dependency from Maven.</p><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;io.rest-assured&lt;/groupId&gt;<br>    &lt;artifactId&gt;json-schema-validator&lt;/artifactId&gt;<br>    &lt;version&gt;4.1.2&lt;/version&gt;<br>&lt;/dependency&gt;</pre><h4>Creating a TestNG DataProvider</h4><p>An important feature provided by TestNG is the TestNG DataProvider feature. It helps you to write data-driven tests which essentially means that the same test method can be run multiple times with different data-sets.</p><p>The following example DataProvider creates a test data object that contains information about the executable tests. This test data object returns from the <em>excelDataReader method.</em></p><pre>@DataProvider(name = <strong>&quot;getApiEndPointData&quot;</strong>)<br>@Parameters({<strong>&quot;filepath&quot;</strong>})<br><strong>public </strong>Iterator&lt;Object[]&gt; getApiTestData(ITestContext context) <strong>throws </strong>IOException {<br>    String dataFile = context.getCurrentXmlTest().getParameter(<strong>&quot;filepath&quot;</strong>);<br>    <strong>return </strong>ExcelReader.<em>excelDataReader</em>(0, dataFile);<br>}</pre><h4>DataProvider in REST Assured tests</h4><p>Using the data specified in the DataProvider in REST Assured tests is fairly straightforward. In the example below, we’re using it in combination with a RESTful web service that uses five query parameter (endpoint, headerName, headerValue, statusCode, schema)</p><pre>@Test(dataProvider = <strong>&quot;getApiEndPointData&quot;</strong>)<br><strong>public void </strong>ExecuteGetApiTestCase(String endpoint,String[] headerName,String[] headerValue, Integer statusCode, String schema) <strong>throws </strong>AssertionError {<br>request.when().get(endpoint)<br>        .then()<br>        .assertThat()<br>        .statusCode(statusCode).and()<br>        .body(<em>matchesJsonSchemaInClasspath</em>(<strong>&quot;schema.json&quot;</strong>));<br>}</pre><p>When we run this test, we can see in the output that the test is run as many times as data provided in the excel sheet.</p><p><strong>Excel Reader With the help of Apache POI library</strong></p><p>With the help of Apache POI library, read excel file and generate data provider object (Iterator&lt;Object[]&gt;). This object contains a list of all tests which have five properties (SERVICE_ENDPOINT, HeaderName, HeaderValue, statusCode, Schema).</p><pre><strong>public static final </strong>Iterator&lt;Object[]&gt; excelDataReader(Integer sheetNumber, String filepath) <strong>throws </strong>IOException {<br><br>    String[] Qparam = <strong>null</strong>;<br>    String[] Qvalue = <strong>null</strong>;<br>    String []  HeaderName = <strong>null</strong>;<br>    String [] HeaderValue = <strong>null</strong>;<br>    String SERVICE_ENDPOINT;<br>    String baseURI;<br>    ArrayList&lt;Object[]&gt; apiData = <strong>new </strong>ArrayList&lt;Object[]&gt;();<br>    DataFormatter formatter = <strong>new </strong>DataFormatter();<br>    File f = <strong>new </strong>File(filepath);<br>    FileInputStream file = <strong>new </strong>FileInputStream(f);<br>    Workbook workbook = <strong>new </strong>XSSFWorkbook(file);<br>    Sheet firstSheet = workbook.getSheetAt(sheetNumber);<br><br>    <strong>for </strong>(Row row : firstSheet) {<br>        <strong>if</strong>(row.getRowNum() == 0)<br>            <strong>continue</strong>;<br>        Cell baseURlCell = row.getCell(2);<br>        Cell UrlCell = row.getCell(5);<br>        Cell QparamCell = row.getCell(3);<br>        Cell QvalueCell = row.getCell(4);<br>        Cell HeaderNameCell = row.getCell(7);<br>        Cell HeaderValueCell = row.getCell(8);<br>        Cell StatusCodeCell = row.getCell(6);<br>        Cell SchemaCell = row.getCell(9);<br>        Cell RequestBodyCell = row.getCell(10);<br>        baseURlCell.setCellType(Cell.<strong><em>CELL_TYPE_STRING</em></strong>);<br>        UrlCell.setCellType(Cell.<strong><em>CELL_TYPE_STRING</em></strong>);<br>        QparamCell.setCellType(Cell.<strong><em>CELL_TYPE_STRING</em></strong>);<br>        QvalueCell.setCellType(Cell.<strong><em>CELL_TYPE_STRING</em></strong>);<br>        HeaderNameCell.setCellType(Cell.<strong><em>CELL_TYPE_STRING</em></strong>);<br>        HeaderValueCell.setCellType(Cell.<strong><em>CELL_TYPE_STRING</em></strong>);<br>        StatusCodeCell.setCellType(Cell.<strong><em>CELL_TYPE_NUMERIC</em></strong>);<br>        SchemaCell.setCellType(Cell.<strong><em>CELL_TYPE_STRING</em></strong>);<br><br>        baseURI = baseURlCell.getStringCellValue() + UrlCell.getStringCellValue();<br><br>        <strong>if </strong>(QparamCell.getStringCellValue() != <strong>&quot;NA&quot; </strong>&amp;&amp; QvalueCell.getStringCellValue() != <strong>&quot;NA&quot;</strong>) {<br>            Qparam = formatter.formatCellValue(QparamCell).split(<strong>&quot;\\|&quot;</strong>);<br>            Qvalue = formatter.formatCellValue(QvalueCell).split(<strong>&quot;\\|&quot;</strong>);<br>        }<br>        <strong>if </strong>((HeaderNameCell.getStringCellValue() != <strong>&quot;NA&quot; </strong>&amp;&amp; HeaderValueCell.getStringCellValue() != <strong>&quot;NA&quot;</strong>)) {<br>            HeaderName = formatter.formatCellValue(HeaderNameCell).split(<strong>&quot;\\|&quot;</strong>);<br>            HeaderValue = formatter.formatCellValue(HeaderValueCell).split(<strong>&quot;\\|&quot;</strong>);<br>        }<br>        SERVICE_ENDPOINT = baseURI;<br>        <strong>if</strong>(Qparam != <strong>null </strong>&amp;&amp; (Qparam[0] != <strong>&quot;NA&quot; </strong>&amp;&amp; Qvalue[0] != <strong>&quot;NA&quot;</strong>)) {<br>            <strong>for </strong>(<strong>int </strong>j = 0; j &lt; Qparam.<strong>length</strong>; j++) {<br>                <strong>if </strong>(j == 0)<br>                    SERVICE_ENDPOINT = baseURI + <strong>&quot;?&quot; </strong>+ Qparam[j] + <strong>&quot;=&quot; </strong>+ Qvalue[j];<br>                <strong>else</strong><br>                    SERVICE_ENDPOINT = SERVICE_ENDPOINT.concat(<strong>&quot;&amp;&quot; </strong>+ Qparam[j] + <strong>&quot;=&quot; </strong>+ Qvalue[j]);<br>            }<br>        }<br>       <br>        <strong>if</strong>(sheetNumber == 0) {<br>            Object ob[] = {SERVICE_ENDPOINT, HeaderName, HeaderValue, (<strong>int</strong>)StatusCodeCell.getNumericCellValue(), SchemaCell.getStringCellValue()};<br>            apiData.add(ob);<br>        }<br>        <strong>else </strong>{<br>            RequestBodyCell.setCellType(Cell.<strong><em>CELL_TYPE_STRING</em></strong>);<br>            Object ob[] =  {SERVICE_ENDPOINT, HeaderName, HeaderValue, (<strong>int</strong>)StatusCodeCell.getNumericCellValue(), SchemaCell.getStringCellValue(),RequestBodyCell.getStringCellValue()};<br>            apiData.add(ob);<br>        }<br>    }<br>    <strong>return </strong>apiData.iterator();<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99a53e590e7a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>