Saturday 12 March 2016

REST API testing using RestAssured

In this post I will be sharing my knowledge on testing REST APIs using RestAssured Framework.

Background: REST APIs can be tested in many ways, like using SOAPUI, Fiddler, PostMan, Chrome client,.. etc..  but these have limitations to automate in real-time testing even though SOAPUI and Fiddler provide some support, when it comes to continuous integration and delivery model organizations move away from tools, instead they use expertise to build their automated test cases using open-source libraries, one such is RestAssured.

you can download this library from here and put that under your build-path via your IDE(Eclipse).
get your REST API method and other header information from your design documents.


package defaultPackage;
import java.util.HashMap;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.path.json.JsonPath;
import com.jayway.restassured.response.Response;
public class TestRestAssured1 {

@Test
public void GetMyStockDetails() {

HashMap<String,String> m=new HashMap<String,String>();
m.put("Accept", "application/json");
m.put("Authorization", "appKeyToken=Framework001&appKey=0xxxxxxxxxxxx0");

           Response res = RestAssured.with().headers(m).get("http://1xx.xx.xx.xx7:8x/v1/Mystock/all/050017924/07A103");

AssertJUnit.assertEquals(200, res.getStatusCode());
String json = res.asString();
System.out.println(json);


System.out.println(res.header("X-CustomMessage"));
JsonPath jp = new JsonPath(json);

System.out.println(jp.get("[0].quantity.unitOfMeasure"));
System.out.println(jp.get("product.description"));

AssertJUnit.assertEquals("Choco", jp.get("product.description"));
AssertJUnit.assertEquals("Packet", jp.get("[0].quantity.unitOfMeasure"));
AssertJUnit.assertEquals("1", jp.get("id"));
}
}


use this code to test your REST API method, just replace the URL and the header details in the above code, Put all your headers(Authorization, Accept, content-type, Language, X-API version etc) inside the map and pass them as a single entity as above.

your json response will be available as string, use it to validate against your expected values,

The concept is same as any http client library usage, but this framework is completely developed for testing, this has enormous apis for validating many things, and also this comes with inbuilt JSON extractor using jsonpath as demoed above. 

You can integrate this with the framework which I've shared on Apache-http client, you just replace the Apache-http code with the above RestAssured code, and use parameterization for driving the tests from an excel or DB.



6 comments:

  1. Hi Karthic,
    can i have your phone no, i need help...

    ReplyDelete
  2. Hi Karthic,

    Nice article! Can you post a short example showing how we can parameterize rest assured based API testing using excel?

    ReplyDelete
  3. thanks for reading,

    kindly look my other blog http://automationextra.blogspot.in/2015/03/hello.html where I've given a simple utility class to read an excel and parameterize to your tests.

    ReplyDelete
  4. Hi karthik,

    could you please use github to fork your code, it is usefull for you and others. thanks for sharing.

    ReplyDelete
  5. HI Karthik, Thanks for such a nice post. Very useful.

    m.put("Authorization", "appKeyToken=Framework001&appKey=0xxxxxxxxxxxx0");
    could you plz provide what are the steps to get the token ?

    ReplyDelete
    Replies
    1. It depends on your application design, in my case it was a dedicated token which is reserved for testing.
      Usually in most applications, you may have to use another GET api call with your UserName+credentials to get the AuthToken and use it in ever other API calls.

      you need to check your API docs, or even possible that your APIs are not authenticated with tokens!!

      Delete