Saturday 3 August 2019

Rest API Automation in Java - Post Method

In this post, we will be using Rest-Assured library and TestNG to automate the http post method of a Rest-ful API. Rest Assured is a Java library using which we can test and validate the REST web services. Although Rest-assured provides its own validating mechanism(assertions for validating response) but we can combine Rest-assured with TestNG to get the best of both the libraries.

During the course of this tutorial, we will be using the following-
  • Sample Rest API to be used in Automation - RestCountries API
  • Rest-Assured and its dependencies (https://code.google.com/p/rest-assured/wiki/Downloads) (Remember to download and add to class path, the other dependencies also). POM dependencies for the Rest Assured and its dependencies-
    <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>2.3.4</version>
    </dependency>
    
    <dependency>
     <groupId>org.codehaus.groovy</groupId>
     <artifactId>groovy-all</artifactId>
     <version>2.3.5</version>
    </dependency>
    
  • TestNG as testing framework

Automating Rest API HTTP Post method

The following code snippet uses requestSpecBuilder to create a post request. We have used the following parameters in our code, you need to set them as per your requirement-
  • APIUrl - Set APIUrl variable with the URL of the Rest API
  • APIBody - Set APIBody variable with body of the Rest API containing parameters e.g. {"key1":"value1","key2":"value2"}
  • setContentType() - Pass the "application/json", "application/xml" or "text/html" etc. headers to setContenType() method
  • Authentication credentials - Pass the username and password to the basic() method and in case of no authentication leave them blank basic("","")
The comments in the following code make it self-explanatory.

@Test
public void httpPost() throws JSONException,InterruptedException {
  
 //Initializing Rest API's URL
 String APIUrl = "http://{API URL}";
  
 //Initializing payload or API body
 String APIBody = "{API Body}"; //e.g.- "{\"key1\":\"value1\",\"key2\":\"value2\"}"
    
 // Building request using requestSpecBuilder
 RequestSpecBuilder builder = new RequestSpecBuilder();
  
 //Setting API's body
 builder.setBody(APIBody);
  
 //Setting content type as application/json or application/xml
 builder.setContentType("application/json; charset=UTF-8");
  
 RequestSpecification requestSpec = builder.build();

 //Making post request with authentication, leave blank in case there are no credentials- basic("","")
 Response response = given().authentication().preemptive().basic({username}, {password})
    .spec(requestSpec).when().post(APIUrl);
 JSONObject JSONResponseBody = new JSONObject(response.body().asString());

 //Fetching the desired value of a parameter
 String result = JSONResponseBody.getString({key});
  
 //Asserting that result of Norway is Oslo
 Assert.assertEquals(result, "{expectedValue}");

 }

No comments:

Post a Comment