Monday, August 4, 2014

Testing rest api's using Rest-Assured

1. Add the following dependencies in your pom.xml
<dependency>
      <groupId>com.jayway.restassured</groupId>
      <artifactId>rest-assured</artifactId>
      <version>2.3.1</version>
      <scope>test</scope>
</dependency>
<dependency>
      <groupId>com.jayway.restassured</groupId>
      <artifactId>json-path</artifactId>
      <version>2.3.1</version>
</dependency>
<dependency>
      <groupId>com.jayway.restassured</groupId>
      <artifactId>json-schema-validator</artifactId>
      <version>2.3.1</version>
      <scope>test</scope>
</dependency>
2. Set the baseURI in @BeforeMethod
RestAssured.baseURI = "http://yoururl.com"; 
RestAssured.port = 80;
3.  In @Test add the following code to get the response from the api
Response response = get("/api/xxx/xxxx");   
          JsonPath jsonpath = new JsonPath(response.asString());
          String ca_id = jsonpath.getString("id");          // id will get all the values from json response and returns a json string. You could validate whatever you are looking for here.

Happy Testing!!!!

No comments:

Post a Comment