- Hamcrest is integrated into JUnit
- Instead of using JUnit’s assertEquals methods, we use Hamcrest’s assert
The following list contains the most important Hamcrest matchers.
-
is - decorator to improve readability
-
allOf - matches if all matchers match (short circuits)
-
anyOf - matches if any matchers match (short circuits)
-
not - matches if the wrapped matcher doesn't match and vice
versa
-
equalTo - test object equality using Object.equals
-
hasToString - test Object.toString
-
instanceOf, isCompatibleType - test type
-
notNullValue, nullValue - test for null
-
sameInstance - test object identity
-
hasEntry, hasKey, hasValue - test a map contains an entry, key
or value
-
hasItem, hasItems - test a collection contains elements
-
hasItemInArray - test an array contains an element
-
closeTo - test floating point values are close to a given
value
-
greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo
- test ordering
-
equalToIgnoringCase - test string equality ignoring case
-
equalToIgnoringWhiteSpace - test string equality ignoring
differences in runs of whitespace
-
containsString, endsWith, startsWith - test string matching
The following assertions are all equivalent:
assertThat(theBiscuit, equalTo(myBiscuit)); assertThat(theBiscuit, is(equalTo(myBiscuit))); assertThat(theBiscuit, is(myBiscuit));