How to use REST assured?

Manoj picture Manoj · Nov 19, 2013 · Viewed 10.1k times · Source

I have never used JUnit or other testing frameworks. All i know is how to develop rest service. I recently saw REST assured framework to test REST api. But all the articles that i found looks like below. But i don't know how to pass request xml and how will i get response and when should i call this method.?

Do i need to use some other tool before this REST assured.? I am completely beginner in this kind of testing frameworks. Please show me some light in this world. All i know is how to send request and check values in the response in SOAPUI. I have never tried this.

expect().
    statusCode(200).
    body(
      "user.email", equalTo("[email protected]"),
      "user.firstName", equalTo("Tim"),
      "user.lastName", equalTo("Testerman"),
      "user.id", equalTo("1")).
    when().
    get("/service/single-user/xml");

Answer

dileepvarma picture dileepvarma · Nov 19, 2013

expect() /* what u expect after sending a request to REST Service */

statusCode(200) /*you are expecting 200 as statuscode which tells request handled successfully at server */

body() /* the conditions given in body are compare the value with expected values. "equalTo" hamcrest matcher condition (you need to have hamcrest jar in java classpath).*/

when(). /* as is name says above all will be done after sending get/post/put/delete request right so before you put these get,post,put,delete you will have this method as prefix */

get("/service/single-user/xml") /* the actual REST API request url goes here. can be GET/POST/PUT/DELETE. the confusion for you is its only showing half part which is base path.you can give entire request url in get() method.*/

more on: http://rest-assured.googlecode.com/svn/tags/1.8.1/apidocs/com/jayway/restassured/RestAssured.html

I hope this helps.