rest assured api testing practice code example

Example 1: rest assured script

The syntax of Rest Assured.io is the most beautiful part, as it is very
BDD like and understandable.

Given(). (lets you set a background)
		param("customer_id", "102").
        header("z", "w").
when(). (marks the premise of your scenario. For ex: get url)
Method(). (replace it with any of CRUD operations like get post put delete)
Then(). (your assert and matcher conditions goes here)
		statusCode(xxx).
        Body("x, "y", equalTo("z"));

Example 2: API/Webservices with RestAssured Library?

import static io.restassured.RestAssured.* ;
URI uri = new URI(" ... / methods(get, post))
GET;
Response response =
given().accept(ContentType.JSON).
when().get(URI);
response.
then().assertThat().statusCode(200).
and().assertThat().ContentType(ContentType.JSON);

POST;
Response response =
given().ContentType(ContentType.JSON).with().
accept(ContentType.JSON).and().body(JSONbody).
when().post(URI);
response.
then().assertThat().statusCode(200);

Tags:

Java Example