Using Hamcrest to Assert in SwitchYard

Updated -

Hamcrest is a framework for writing matcher objects. It allows you to define match rules declaratively. Use Hamcrest’s assertThat construct and the standard set of matchers, both of which you can statically import:

import static org.hamcrest. MatcherAssert .assertThat;
​import static org.hamcrest. Matchers .*;

Hamcrest comes with a library of useful matchers, such as:

  • Core
    • anything: Always matches, useful if you do not want to know what the object under test is
    • describedAs: Decorator for adding custom failure description
    • is: Decorator to improve readability
  • Logical
    • allOf: Matches if all matchers match, short circuits (like && in Java)
    • anyOf: Matches if any matchers match, short circuits (like || in Java)
    • not: Matches if the wrapped matcher does not match and vice versa
  • Object
    • equalTo: Test object equality using Object.equals
    • hasToString: Test Object.toString
    • instanceOf, isCompatibleType: Test type
    • notNullValue, nullValue: Test for null
    • sameInstance: Test object identity
  • Beans
    • hasProperty: Test JavaBeans properties
  • Collections
    • array: Test an array’s elements against an array of matchers
    • 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
  • Number
    • closeTo: Test floating point values are close to a given value
    • greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo: Test ordering
  • Text
    • equalToIgnoringCase: Test string equality ignoring case
    • equalToIgnoringWhiteSpace: Test string equality ignoring differences in runs of whitespace
    • containsString, endsWith, startsWith: Test string matching

Comments