Thursday, June 26, 2014

REST API Testing using REST-assured

This is my second post about doing Automation of REST API Testing, few days back I write the post to do REST API Testing using Frisby.js ( a Javascript tool), today i will post about how to do this in JAVA using REST-assured API.

Installation
Please down and put following JAR files in your build path. 
rest-assured
json-schema-validator

Test Case. 
For this demo purpose, I am going to use freegeoip.net site which return simple JSON response. 
steps: 
1. visit http://freegeoip.net:80/json/yahoo.com site 

verification:
1. verify that it returns 200 OK 
2. verify it return correct JSON 

Test Script. 
/**
 * @author Gaurang_Shah
 *
 */

//import com.jayway.restassured.RestAssured;
import static com.jayway.restassured.RestAssured.baseURI;
import static com.jayway.restassured.RestAssured.given;
import static com.jayway.restassured.RestAssured.useRelaxedHTTPSValidation;
import static com.jayway.restassured.RestAssured.when;
import static org.hamcrest.Matchers.equalTo;

import org.junit.Test;

import com.jayway.restassured.authentication.FormAuthConfig;

public class TestATP {

 
 @Test
 public void testDemo(){
  baseURI="http://freegeoip.net:80/";
  when().
    get("/json/yahoo.com"). 
  then().
    assertThat().statusCode(200).
    log().all().
    body("country_code",equalTo("US"),
      "country_name",equalTo("United States"),
      "region_code",equalTo("CA"),
      "region_name",equalTo("California"),
      "city",equalTo("Sunnyvale")
      
     );
 
 }
}