Hamcrest
Anubhav Goyal
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
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.
Dependency
dependencies{
test 'junit:junit:4.12'
test 'org.hamcrest:hamcrest-library:1.3'
}
Hamcrest can be used with JUnit 3 and 4 and TestNG.
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.
We need to use following two static imports :
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
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.
● 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.
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.
● 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.
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
References
● https://www.leveluplunch.com/java/examples/#java-hamcrest
● https://code.google.com/archive/p/hamcrest/wikis/Tutorial.wiki
● http://www.vogella.com/tutorials/Hamcrest/article.html
● https://github.com/hamcrest/JavaHamcrest
● https://objectpartners.com/2013/09/18/the-benefits-of-using-assertthat-over-other-assert-methods-in-
unit-tests/
Source Code Link:
https://github.com/NexThoughts/Hamcrest
Thanks

Hamcrest

  • 1.
  • 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 Hamcrestis 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.
  • 4.
  • 5.
    How to useOf 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 touse following two static imports : import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*;
  • 7.
    Benefit of assertThatover 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: ● Hamcrestlibrary 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 HamcrestMatchers 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 CustomMatchers Hamcrest provide us functionality of creating matchers our own. We can create own matchers by two way : ● Using FeatureMatcher ● Using TypeSafeMatcher
  • 12.
    References ● https://www.leveluplunch.com/java/examples/#java-hamcrest ● https://code.google.com/archive/p/hamcrest/wikis/Tutorial.wiki ●http://www.vogella.com/tutorials/Hamcrest/article.html ● https://github.com/hamcrest/JavaHamcrest ● https://objectpartners.com/2013/09/18/the-benefits-of-using-assertthat-over-other-assert-methods-in- unit-tests/ Source Code Link: https://github.com/NexThoughts/Hamcrest
  • 13.