Hamcrest
Shruti Kuratkar
Innovative Frontend Developer | Expert in React, JavaScript, HTML, CSS| Skilled in Bootstrap & CSS Frameworks | Experience with Express & Node.js | Familiar with RESTful APIs | Agile | Git & Bitbucket | Jira
Hello Everyone,
Here is small concept of hamcrest, Where to use, what's the purpose, Hamcrest Vs TestNG, Advantages. I explained this concept through example.Very easy to understand. Have a look...
We have to add dependency in our pom.xml file
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-library -->
<dependency>
??<groupId>org.hamcrest</groupId>
??<artifactId>hamcrest-library</artifactId>
??<version>1.3</version>
??<scope>test</scope>
</dependency>
HAMCREST ASSERTION
======================
-> Hamcrest is a well known assertion library used for unit testing along with JUnit.
-> Hamcrest can be used along with Rest Assured for assertions.
-> Uses matcher classes for making assertions
Adv:
=====
-> Human readable and in plain english
-> Code is neat and intuitive
-> Provides thin methods like "is" and "not", also called as decorators, for more readibility
Hamcrest Vs TestNG
====================
-> Readibility
-> Descriptive error messages ( When ever there is an assertion failure ,the hamcrest will display the cause of? failure and it will output the expected and the actual results. TestNg will not provide this info)
-> Type Safety ( Ex : In hamcrest ,if you try to match an integer with string , you will get an error during compile time itself .? TestNg will not provide this info )
Collection matchers (List, Array, Map, etc.)
==============================================
hasItem() -> check single element in a collection
not(hasItem()) -> check single element is NOT in a collection
hasItems() -> Check all elements are in a collection
contains() -> Check all elements are in a collection and in a strict order
containsInAnyOrder() -> Check all elements are in a collection and in any order
empty() -> Check if collection is empty
not(emptyArray()) -> Check if the Array is not empty
hasSize() -> Check size of a collection
everyItem(startsWith()) -> Check if every item in a collection starts with specified string
hasKey() -> Map -> Check if Map has the specified key [value is not checked]
hasValue() -> Map -> Check if Map has at least one key matching specified value
hasEntry() -> Maps -> Check if Map has the specified key value pair
equalTo(Collections.EMPTY_MAP) -> Maps [Check if empty]
allOf() -> Matches if all matchers matches
anyOf() -> Matches if any of the matchers matches
Numbers:
greaterThanOrEqualTo()
lessThan()
lessThanOrEqualTo()
String:
containsString()
emptyString()
Examples :
package com.rest;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.equalTo;
领英推荐
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import org.testng.annotations.Test;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
public class HamcrestAssert{
@Test
public void hamcrest_assert_on_extracted_response() {
String name =
given().
? ? baseUri("https://api.getpostman.com").
? ? header("X-Api-Key","PMAK-62dd3be1f2197b673bb9313e-311678da8c49f1b5d28f37e348b64fd6ee").
when().
? ? get("/collections").
then().???
? ? assertThat().???
? ? statusCode(200).
? ? extract().
? ? response().path("collections[0].name");
System.out.println("Collection Name :" +name);
assertThat(name,equalTo("Employee Controller"));? // hamcrest assertion
//Assert.assertEquals(name,"Employee Controller");? ? // testNg assertion
}
???????@Test
public void hamcrest_assert_on_extracted_response1()
{
//contains method
? given().
? ? baseUri("https://api.getpostman.com").
? ? header("X-Api-Key","PMAK-62dd3be1f2197b673bb9313e-311678da8c49f1b5d28f37e348b64fd6ee").
? ? when().
? ? get("/collections").
? then().
? assertThat().
? statusCode(200).
? body("collections.name",contains("Employee Controller","New Collection", "New Collection","Git_Api_Suite","rmgyantra2", "testCaseAssignment")
?????);
}
@Test
public void hamcrest_assert_on_extracted_response2()
{
? given().
? ? baseUri("https://api.getpostman.com").
? ? header("X-Api-Key","PMAK-62dd3be1f2197b673bb9313e-311678da8c49f1b5d28f37e348b64fd6ee").
???when().
? ? get("/collections").
? then().
? assertThat().
? statusCode(200).
? body("collections.name",containsInAnyOrder("Employee Controller","New Collection", "New Collection","Git_Api_Suite","rmgyantra2", "testCaseAssignment"),
? "collections.name",is(not(emptyArray())), "collections.name",hasSize(6),
? "collections.name", allOf(startsWith("Employee"), containsString(""))
????);
?}
}
I hope you got clarity on hamcrest assertions, how to use...All the best!
Do more practice on...
Thank You!
Published By: Shruti Kuratkar
Date: Sun,19 Feb 2023