How to automate RestAssured test with Java

How to automate RestAssured test with Java

In this post, I will show a quick example of how we can automate API Testing with RestAssured using java. For this example, I will use https://samples.openweathermap.org and show how we can call the weather details.

First, we need to create a maven project on eclipse. Once this project is created we need to add the dependencies. In my example, I have included the rest assured dependency as well as the TestNG dependency as shown below.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>restAssureProject</groupId>
   <artifactId>training</artifactId>
   <version>1.0</version>
   <dependencies>
      <dependency>
         <groupId>org.hamcrest</groupId>
         <artifactId>hamcrest-all</artifactId>
         <version>1.3</version>
      </dependency>
      <dependency>
         <groupId>org.hamcrest</groupId>
         <artifactId>hamcrest-junit</artifactId>
         <version>2.0.0.0</version>
      </dependency>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.12</version>
      </dependency>
      <dependency>
         <groupId>com.jayway.restassured</groupId>
         <artifactId>json-schema-validator</artifactId>
         <version>2.8.0</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
      <dependency>
         <groupId>io.rest-assured</groupId>
         <artifactId>rest-assured</artifactId>
         <version>4.3.0</version>
         <scope>test</scope>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.testng/testng -->
      <dependency>
         <groupId>org.testng</groupId>
         <artifactId>testng</artifactId>
         <version>7.1.0</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
</project>


Next, we will create a package and a class file. Within this class file, we will assert the response for a GET API call using the TestNG assertion.


package MyPackage;


import io.restassured.RestAssured;
import io.restassured.response.Response; 
import io.restassured.specification.RequestSpecification; 
import org.testng.Assert; 
import org.testng.annotations.Test; 
import io.restassured.http.ContentType; 
import static io.restassured.RestAssured.*; 
import static org.hamcrest.Matchers.*; 
import io.restassured.parsing.Parser;
 
public class GetData {


    public static Response responseValidation(String endpoint) {

        RestAssured.defaultParser = Parser.JSON;
        return  given().headers("Content-Type", ContentType.JSON, "Accept", ContentType.JSON).
                        when().get(endpoint).
                        then().contentType(ContentType.JSON).extract().response();
    }

    @Test 
    public void validateResponse() {
        Response response = responseValidation("http://restapi.demoqa.com/utilities/weather/city/Colombo");

        String responseValue = response.jsonPath().getString("City"); 
        Assert.assertEquals(responseValue, "Colombo");    }
}

As shown from the above example, we call the API  endpoint and then assert if the endpoint returns a response with the city as "Colombo"

If we want to simply assert the status code we could write our test as below. Here, we simply call the base URL and base path and then assert if the response returns a code of 200

@Testpublic void testWithKey() {

                given() .baseUri("http://restapi.demoqa.com/")
                        .basePath("utilities/weather/city/Colombo")
                        .contentType(ContentType.JSON)
                .when().get("/city/{cityName}","Colombo")
                .then()
                       .statusCode(200);



Comments