Contact Us 1-800-596-4880

MUnit Matchers

MUnit matchers are a set of DataWeave functions to define assertion conditions for any value in an expression.

When defining matchers, include the prefix MunitTools:: in the expression, as illustrated in the following examples.

Matchers are grouped according to the type of conditions you want to validate.

Match Core Values

Core matchers evaluate core values in the expression:

  • Is the expression null?

  • What is the media type or encoding of the expression?

  • Does the expression return false?

  • Are two evaluations successful?

Core Matchers:

  • nullValue() checks that the expression is null.

    Example:

    #[MunitTools::nullValue()]
  • notNullValue() checks that the expression is not null.

    Example:

    #[MunitTools::notNullValue()]
  • withMediaType(String) checks that the expression’s media type is the one specified.

    Example:

    #[MunitTools::withMediaType('text/xml')]
  • withEncoding(String) checks that the expression’s encoding is the one specified.

    Example:

    #[MunitTools::withEncoding('UTF-8')]
  • both(Matcher, Matcher) checks that both provided matchers are successful.

    Example:

    #[MunitTools::both(MunitTools::notNullValue(), MunitTools::equalTo('example'))]
  • either(Matcher,Matcher) checks that at least one of the matchers is successful.

    Example:

    #[MunitTools::either(MunitTools::nullValue(), MunitTools::equalTo(0))]
  • not(Matcher) checks if the provided matcher is not successful.

    Example:

    #[MunitTools::not(MunitTools::equalTo(0))]
  • anyOf(Matchers[]) checks if any of the matchers are successful.

    Example:

    #[MunitTools::anyOf([MunitTools::notNullValue(),MunitTools::withMediaType('text/xml'),MunitTools::isEmptyString()])]
  • allOf(Matchers[]) checks if all of the matchers are successful.

    Example:

    #[MunitTools::allOf(MunitTools::notNullValue(),MunitTools::withMediaType('text/xml'),MunitTools::isEmptyString())]

Match Strings

String Matchers perform assertions on String expressions:

  • Does the expression contain a specified string?

  • Does the expression start or end with a specified string?

  • Does the string in the expression have zero length?

  • Does the string in the expression match a specified string?

String Matchers:

  • containsString(String) checks that the expression contains the specified String.

    Example:

    #[MunitTools::containsString('example')]
  • startsWith(String) checks that the expression starts with the specified String.

    Example:

    #[MunitTools::startsWith('exam')]
  • endsWith(String) checks that the expression ends with the specified String.

    Example:

    #[MunitTools::endsWith('ple')]
  • isEmptyString() checks that the expression has zero length.

    Example:

    #[MunitTools::isEmptyString()]
  • isEmptyOrNullString() checks that the expression is null, or has zero length.

    Example:

    #[MunitTools::isEmptyOrNullString()]
  • equalToIgnoringCase(String) checks that the expression is equal to the specified String, ignoring case.

    Example:

    #[MunitTools::equalToIgnoringCase('example')]
  • equalToIgnoringWhiteSpace(String) checks that the expression is equal to the string disregarding leading and trailing white spaces, and compressing all inner white spaces to a single space.

    Example:

    #[MunitTools::equalToIgnoringWhiteSpace('An Example')]
  • stringContainsInOrder(String, String,…​) checks that the expression contains all of the specified substrings, regardless of the order of their appearance.

    Example:

    is="#[MunitTools::stringContainsInOrder(['an', 'example'])]"/>

Match Comparable Expressions

Comparable matchers compare the expression against a specified value:

  • Is the expression’s value bigger or smaller than a specified value?

  • Is the expression’s value closer to a specific number?

Some comparable matchers take a Comparable type. Comparable is a DataWeave type that represents all the types that can be compared to each other. Allowed primitives are String, Number, Boolean, DateTime, LocalDateTime, LocalTime, Time, and TimeZone.

Comparable Matchers:

  • greaterThan(Comparable) checks that the expression is greater than the specified value.

    Examples:

    #[MunitTools::greaterThan(20)]
    #[MunitTools::greaterThan(|2017-08-09|)]
  • greaterThanOrEqualTo(Comparable) checks that the expression is greater than or equal to the specified value.

    Examples:

    #[MunitTools::greaterThanOrEqualTo(20)]
    #[MunitTools::greaterThanOrEqualTo(|2017-08-09|)]
  • lessThan(Comparable) checks that the expression is less than the specified value.

    Examples:

    #[MunitTools::lessThan(20)
    #[MunitTools::lessThan(|2017-08-09|)]
  • lessThanOrEqualTo(Comparable) checks that the expression is less than or equal to the specified value.

    Examples:

    #[MunitTools::lessThanOrEqualTo(20)]
    #[MunitTools::lessThanOrEqualTo(|2017-08-09|)]
  • closeTo(Number, Number) checks that the expression is in the range defined by the first and second number.

    Example:

    #[MunitTools::closeTo(1, 0.01)]
  • equalTo(Object) checks that the expression is equal to a specified value. This matcher also accepts DataWeave objects.

    Examples:

    #[MunitTools::equalTo('example')]
    #[MunitTools::equalTo({example1: 1 , example2: 2}]

Match Maps and Iterations

Use iterable matchers and maps matchers when your expression is an array or a map of data:

  • Is the map or array in the expression empty?

  • Does the map or array in the expression have a specific key?

  • Is every item in the array from the expression greater than or less than a specific number?

  • Does any item in the array from the expression start or end with a specified string?

Iterable and Map Matchers:

  • everyItem(Matcher) checks that every element in the expression matches the specified matcher. This matcher works only for arrays.

    Examples:

    #[MunitTools::everyItem(MunitTools::notNullValue())]
    #[MunitTools::everyItem(MunitTools::startsWith('a'))]
  • hasItem(Matcher) checks that any element in the expression matches the specified matcher. This matcher works only for arrays.

    Examples:

    #[MunitTools::hasItem(MunitTools::notNullValue())]
    #[MunitTools::hasItem(MunitTools::startsWith('a'))]
  • hasSize(Matcher) checks that the size of the expression matches the specified matcher.

    Examples:

    #[MunitTools::hasSize(MunitTools::equalTo(5))]
    #[MunitTools::hasSize(MunitTools::greaterThan(5))]
  • isEmpty() checks that the expression is an empty collection.

    Examples:

    #[MunitTools::isEmpty()]
  • hasKey(Matcher) checks that the expression has a key that matches the specified matcher. This matcher works only for maps.

    Examples:

    #[MunitTools::hasKey(MunitTools::equalTo('myKey'))]
    #[MunitTools::hasKey(MunitTools::startsWith('a'))]
  • hasValue(Matcher) checks that the expression has a value that matches the specified matcher. This matcher works only for maps.

    Examples:

    #[MunitTools::hasValue(MunitTools::equalTo('myValue'))]
    #[MunitTools::hasValue(MunitTools::startsWith('a'))]