Hamcrest

NexThoughts 394 views 13 slides Aug 04, 2017
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

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 use...


Slide Content

Hamcrest Anubhav Goyal

Contents: About Hamcrest Dependency How to use Hamcrest? Benefit of assertThat over assertEquals Overview of Hamcrest Matchers OverView Of Custom Matchers 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