Assertion of JSON in Rest Assured using Hamcrest

HOME

What is Assertion?

Assertion is a way to verify that the expected result and the actual result matches or not in the test case.  A test is considered successful ONLY if it is completed without throwing any exception. If the current value and the expected value match then the assertion passes and when the assertion passes nothing happens. But when an assertion fails it will fail the test case.

There are various ways to perform assertions in API Testing. For API Testing, we are using Rest Assured which uses either Hamcrest or JUnit assertions. We are going to discuss Hamcrest Assertions here.

What is Hamcrest?

Hamcrest is a framework for writing matcher objects allowing ‘match’ rules to be defined declaratively. We do not need to add Hamcrest depdendency explicitly as Rest-Assured 4.3.3 version include by itself. To know more about Hamcrest, please refer this link.

Below is an example of JSON Response. I will perform various assertions on this JSON Response.

To use hamcrest assertion please import the Matchers class static member.

Number related assertions

  1. equalTo – It checks whether the retrieved number from response is equal to the expected number.
  2. greaterThan – checks extracted number is greater than the expected number.
  3. greaterThanOrEqualTo – checks whether the extracted number is greater than equal to the expected number.
  4. lessThan – It checks whether the retrieved number from response is lesser than to the expected number.
  5. lessThanOrEqualTo – It checks whether the retrieved number from response is lesser than or equal to the expected number.

Below assertions are imported from package shown below:-

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.lessThan;

Below are the examples to show the use of number related assertions.

public class HamcrestExample {

	public String endpoint = "https://restful-booker.herokuapp.com/booking/1";

	@Test
	public void numberAssertions() {
		RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint).then()
                   .body("totalprice", equalTo(813));

		RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint)
                   .then().body("totalprice",greaterThan(500));

		RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint)
                   .then().body("totalprice",greaterThanOrEqualTo(500));

		RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint)
                   .then().body("totalprice",lessThan(1000));

		RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint)
                   .then().body("totalprice",lessThanOrEqualTo(1000));

	}
}

String related Assertions

  1. equalTo – It checks whether the extracted string from JSON is equal to the expected string.
  2. equalToIgnoringCaseIt checks whether the extracted string from JSON is equal to the expected string without considering the case (small or capital).
  3. equalToIgnoringWhiteSpace – It checks whether the extracted string from JSON is equal to the expected string by considering the white spaces.
  4. containsString – It checks whether the extracted string from JSON contains the expected string as a substring.
  5. startsWith It checks whether the extracted string from JSON is starting with a given string or character.
  6. endsWith It checks whether the extracted string from JSON is ending with a given string or character.

Below assertions are imported from package shown below:-

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;

Below are the examples to show the use of string related assertions.

public class HamcrestExample {

	public String endpoint = "https://restful-booker.herokuapp.com/booking/1";

	@Test
	public void stringAssertions() {
		RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint)
                   .then().body("firstname",equalTo("Mark"));

		RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint)
                   .then().body("firstname",equalToIgnoringCase("MARk"));

		RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint)
                   .then().body("firstname",containsString("Mark"));

		RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint)
                   .then().body("firstname",startsWith("M"));

		RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint)
                   .then().body("firstname",endsWith("k"));

        RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint)
                   .then().body("firstname",equalToIgnoringWhiteSpace("   Mark "));


	}
}
  1. nullValue – It checks whether the extracted response from JSON is NULL or Not.
  2. hasKey – It checks whether the extracted map has an expected key.

Below assertions are imported from package shown below:-

import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

Below are the examples to show the use of collection related assertions.

public class HamcrestExample {

	public String endpoint = "https://restful-booker.herokuapp.com/booking/1";

	@Test
	public void collectionAssertions() {
		RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint)
                   .then().body("totalprice1",is(nullValue()));

		RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint)
                   .then().body("bookingdates",hasKey("checkin"));

	}
}

Not Assertion

The not assertion inverts the meaning of the other assertions. For example, if you want to perform negative assertions, then we can use any assertions with NOT.

Below assertion is imported from package shown below:-

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;

Below are the examples to show the use of negative assertions.

public class HamcrestExample {

	public String endpoint = "https://restful-booker.herokuapp.com/booking/1";

	@Test
	public void negativeAssertions() {
		RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint)
                   .then().body("totalprice",not(equalTo(874)));

	}
}

Multiple Assert Statements

In the below exaple, all 3 asserstions will fail, but it will only execute assertion and first assertion is failed, then other assertions will not be executed.

public class HamcrestExample {

	public String endpoint = "https://restful-booker.herokuapp.com/booking/1";

	@Test
	public void test() {
		RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint).then()
                   .body("firstname", equalTo("Jim")) // will fail
				   .body("lastname", equalTo("Smith")) // will fail
				   .body("totalprice", equalTo(314)); // will fail
	}

}

To execute all the assertions present in the test case we have to combine all the assertion into a single body, just like below. You can see that all the assertions are failed and they are shown in the response.

public class HamcrestExample {

	public String endpoint = "https://restful-booker.herokuapp.com/booking/1";

	@Test
	public void test() {
		RestAssured.given().contentType(ContentType.JSON)
                   .when().get(endpoint).then()
                   .body("firstname", equalTo("Jim"), // will fail																												
				   "lastname", equalTo("Smith"), // will fail
				   "totalprice", equalTo(314)); // will fail
	}

}

I have tried to show the use of few of the most commonly used assertion methods. There are many more methods avilable in Hamcrest package. To know about other methods, write import static org.hamcrest.Matchers and add (.) at the end, it will show the list of all the methods available in Hamcrest.

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s