Rest Assured Testing

Introduction:

Unit Testing is done in order to test the individual Java classes in an application but there is also a need to carry out Functional Test in order to test the application end to end apart from the Integration Test which focuses on testing the modules. Rest assured testing thus enables us to test a RESTful application by connecting to the HTTP end points supported by the application and imitate the role of another client/browser. In other words, REST-assured is a way to automate testing of a REST Api.

Structure of a Rest Assured Test

The way Rest Assured Tests are written is in given(), when(), then() format.

1. A sample test code for a testing a GET end point in order to get users:


@Test
public void getUser() {
given()
      .contentType(ContentType.JSON) 
      .port(port). // HTTP port that got allocated at runtime. 
when()
      .get("/users/"). // GET endpoint in order to get the users
then()
      .statusCode(200) // RESPONSE expected
      .body("id", equalTo(128));
}

2. A sample test code for a testing a POST end point in order to add a user:


@Test
public void testAddUser() {
given()
      .contentType(ContentType.JSON)
      .port(port)
      .queryParam("empName", "Andy Murray") // parameters 1
      .queryParam("salary", 50000) // parameters 2 
      .queryParam("Id", 128). // parameters 3 
when()
      .post("/users/add"). //POST endpoint for adding user
then()
      .statusCode(200);
}

3. A sample test code for a testing a DELETE end point in order to remove a user with a specific Id:


@Test
public void testRemoveUserById() {

given()
.contentType(ContentType.JSON)
.port(port).
when()
.delete("/users/remove/{id}", 128). // DELETE end point
then()
.statusCode(200);
}

OR

given()
.contentType(ContentType.JSON)
.port(port)
.pathParam("id", 128). // path parameters specified here
when()
.delete("/users/remove/{id}"). // DELETE end point
then()
.statusCode(200);
}

Why Rest Assured?

  1. Easy HTTP Request Building and Execution: REST-assured allows us to easily define many things such as headers, query parameters, path parameters, request body etc.
  2. Easy Response Checking: REST-assured easily allows us to parse responses by providing constructs for making assertions on response body, response header etc.
  3. Ability to write clean code: REST-assured with its given() – when() – then() style helps to write and understand a code very easily. Predetermined things are under given(), condition under test is specified under when(), and the verification is done under then().

Complete Code Base:

Complete Example can be seen at my git repository: Rest Assured Test Complete Example

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s