This document discusses Hamcrest, a framework for writing matcher objects that allow match rules to be defined declaratively. It can be used with JUnit and TestNG for writing flexible tests. The document outlines how to use Hamcrest with assertThat instead of assertEquals for more readable and flexible testing. It also provides an overview of the different types of matchers in Hamcrest, including object, collection, core, number, and text matchers. Finally, it discusses how to create custom matchers using FeatureMatcher or TypeSafeMatcher.
2. Contents:
1. About Hamcrest
2. Dependency
3. How to use Hamcrest?
4. Benefit of assertThat over assertEquals
5. Overview of Hamcrest Matchers
6. OverView Of Custom Matchers
7. Reference
3. What is Hamcrest
Hamcrest is a framework for writing matcher objects allowing 'match' rules to be defined declaratively.
There are a number of situations where matchers are invaluble, such as UI validation, or data filtering, but
it is in the area of writing flexible tests that matchers are most commonly used.
5. How to use Of Hamcrest
In traditional approach we use :
assetEquals(expacted Value, actual value)
But in Hamcrest , syntax is little differ:
assertThat(actualValue, *is(expected Value))
● * It is method, there are few more method mentioned in next slides.
6. We need to use following two static imports :
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
7. Benefit of assertThat over
assertEquals● Readability:
● The first benefit is that assertThat is more readable than the other assert methods.For example:
● assertThat(actual, is(equalTo(expected))) is much readable then the assertEquals(expected, actual).
● Better Failure Messages:
● assertThat provide much better and readable failure message ,if test case fail.
● Type Safety:
● assertThat is it’s generic and type-safe.
● Flexibility:
● We can use logical matchers. Which makes it flexible and easy to use.
8. ● Portability:
● Hamcrest library is its portability. It can be used with both JUnit and TestNG. JUnit has the
assertThat method, but hamcrest has its own assertThat method that does the same thing
● Custom Matchers:
● Hamcrest provides us custom matchers.
9. Overview of Hamcrest Matchers
There are some following matchers:
● XML Matchers :
● It contains only hasXPath() matcher.
● Bean Matchers:
● It contains hasProperty(propertyName), hasProperty(propertyName, equalTo(value)),
samePropertyValuesAs(obj) matchers.
● Object Matchers:
● This matchers used for testing regarding of Object. We can test for null, instanceOf,
hasToString, equals matchers.
10. ● Collection Matchers:
● It contains list and map related matchers. It provides number of methods for testing list and
map, like hasSize(), contains(), containsInAnyOrder() etc.
● Core Matchers:
● It contains some logical(like work as logical operator in java) and core(basics) matchers, like
allOf(), anyOf(),both(),anything() etc.
● Number Matchers:
● This matcher is used for test numeric type test cases.
● Text Matchers:
● This matcher is used for testing the String type test-cases.
11. Overview Of Custom Matchers
Hamcrest provide us functionality of creating matchers our own. We can create own matchers by two way
:
● Using FeatureMatcher
● Using TypeSafeMatcher