Comparing responses using hamcrest library

Next lets discuss hamcrest librabry.We have seen above that we can compare an actual result we got from server with expected result we put in a text file. We also noticed that we can check various things like if actual result contains extra fields like id. If yes then json assertions that are non-extensible or strict will fail.Same way we can check ordering in a particular array and see that tests using json assertions like strict or strict array will fail if ordering is not same

So we see json assertions are good for all those things.But what if we want to validate an actual response value. Like we want to validate that firstName should be John .Can we do that with jsonassertions. The answer is yes

We can try something like this

String expectedFirstName = “John”
String actualFirstName = given().when.get("https://localhost/8080/student/1").then().extract().path(“firstName”);

Then we can get the actual first name like this and compare them

JSONAssert.assertEquals(expectedFirstName, actualFirstName,JSONCompareMode.LENIENT);

But thing is if we want to compare a hell lot of things like last name ,number of items etc then json assertions become combersome. So in that case hamcrest libraries are a much better option.So we will be discussing various methods to be used In hamcrest libraries below.

For this we need the following dependencies

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-all</artifactId>
    <version>1.3</version>
  </dependency>

And we can compare our response using hamcrest as below. We will see a few test cases here

To check if body has ipoD in the response somewhere or not.

@Test
public void test001(){
        
//Checking in body that key named query should have body equal to iPOD
given().get("/search").then().body("query", equalToIgnoringCase("IPoD"));
}

To check if no of numitems = 10. Here numItems is another key in our response

@Test
public void test002(){
    given().get("/search").then().body("numItems", equalTo(10));
}

Below we will check all maps named items and look for a key name that has a value as given below.If one of the maps named items has key as name and value as below it will pass.

@Test
public void test003(){
given().get("/search").then().body("items.name",hasItem("Apple iPlod touch 16GB"));
}

Below method will check all maps named items and look for a key name that has a values as given below.If few maps named items has key as name and values as the values from below it will pass. We are using hasItems instead of hasItem below to check for multiple items.

@Test
public void test004(){
given().get("/search").then().body("items.name",hasItems("Apple iPod touch 16GB","Refurbished Apple iPod Touch 32GB, 5th Generation, Blue","Apple iPod Touch 6th Generation 32GB Refurbished"));
}

To check if a map named giftOptions in items no 0 has a key as allowGiftWrap and value as false

@Test
public void test005(){
given().get("/search").then().body("items[0].giftOptions", hasEntry ("allowGiftWrap",false));
}

To check if a map named giftOptions in items no 0 has a key as allowGiftWrap

@Test
public void test006(){
    given().get("/search").then().body("items[0].giftOptions",hasKey("allowGiftWrap"));
}