JUnit – assertThat (Hamcrest) – part 2

Let’s continue (you can find Hamcrest matchers – part 1 here) our series of examples how to write JUnit test with Hamcrest matchers usage.

  • comparesEqualTo()

Creates a matcher of a Comparable object that matches when the examined object is equal to the specified value, as reported by the compareTo method of the examined object. The value which, when passed to the compareTo method of the examined object, should return zero.

 

  • contains()

Various matchers which can be used to check if an input Iterable contains values. The order of the values is important and the number of items in the Iterable must match the number of values being tested.

Test if the input list contains all of the values, the order of items is important.

Test if the input list contains items which match all of the matchers in the input matchers list, the order of items is important.

 

  • containsInAnyOrder()

This one is like contains() but as the name suggest the order of items is not important.

 

  • containsString()

A matcher that matches if the examined String contains the specified String anywhere.

 

  • empty()

A matcher for Collections matching examined collections whose isEmpty method returns true.

 

  • emptyArray()

A matcher for arrays that matches when the length of the array is zero.

 

  • emptyCollectionOf()

A matcher for Collections matching examined collections whose isEmpty method returns true.

 

  • emptyIterable()

A matcher for Iterables matching examined iterable that yield no items.

 

  • emptyIterableOf()

A matcher for Iterables matching examined iterable that yield no items and is of the given type.

 

 

Leave a Reply