Nav

About MUnit Matchers

MUnit matchers are a set of DataWeave functions to define assertion conditions in terms of general values instead of specific hardcoded values.

When defining matchers you need to add the MunitTools:: prefix to the expression.

Matchers are grouped according to the type of conditions you want to validate.
You can find:

Core Matchers

These matchers evaluate core values in the expression.
You can use these matchers to evaluate:

  • Whether the expression is null or not.

  • Media Type or Encoding of the expression.

  • Whether the returns false.

  • Whether two evaluations were successful.

These matchers are:

Matcher Description Example

Checks that the expression is null.

  • #[MunitTools::nullValue()]

Checks that the expression is not null.

  • #[MunitTools::notNullValue()]

Checks that the expression’s media type is the one specified.

  • #[MunitTools::withMediaType('text/xml')]

Checks that the expression’s encoding is the one specified.

  • #[MunitTools::withEncoding('UTF-8')]

Checks that both provided matchers are successful.

  • #[MunitTools::both(MunitTools::notNullValue(),MunitTools::equalTo('example'))]

Checks that at least one of the matchers is successful.

  • #[MunitTools::either(MunitTools::nullValue(),MunitTools::equalTo(0))]

Checks if the provided matcher is not successful.

  • #[MunitTools::not(MunitTools::equalTo(0))]

Checks if any of the matchers are successful.

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

Checks if all of the matchers are successful.

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

String Matchers

These Matchers are meant to perform assertions over String expressions.
You can use these matchers to evaluate:

  • Whether the expression contains a specific String.

  • Whether the expression starts or ends with a specific String.

  • Whether the String in the expression has zero length.

  • Compare the String in the expression against a String determined by you.

These matchers are:

Matcher Description Example

Checks that the expression contains the specified String.

  • #[MunitTools::containsString('example')]

Checks that the expression starts with the specified String.

  • #[MunitTools::startsWith('exam')]

Checks that the expression ends with the specified String.

  • #[MunitTools::endsWith('ple')]

Checks that the expression has zero length.

  • #[MunitTools::isEmptyString()]

Checks that the expression is null, or has zero length.

  • #[MunitTools::isEmptyOrNullString()]

Checks that the expression is equal to the specified String, ignoring case.

  • #[MunitTools::equalToIgnoringCase('Example')]

Checks that the expression is equal to the string disregarding leading and trailing white spaces, and compression all inner white spaces to a single space.

  • #[MunitTools::equalToIgnoringWhiteSpace('An Example')]

Checks that the expression contains all of the specified substrings, regardless of the order of their appearance.

  • #[MunitTools::stringContainsInOrder('an', 'example')]

Comparable Matchers

These matchers compare the expression against any provided Value.
You can use these matchers to evaluate:

  • Whether the expression’s value is greater or smaller than a specified value.

  • Whether the expression’s value is 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

These matchers are:

Matcher Description Example

Checks that the expression is greater than the specified value.

  • #[MunitTools::greaterThan(20)]

  • #[MunitTools::greaterThan(|2017-08-09|)]

Checks that the expression is greater than or equal to the specified value.

  • #[MunitTools::greaterThanOrEqualTo(20)]

  • #[MunitTools::greaterThanOrEqualTo(|2017-08-09|)]

Checks that the expression is less than the specified value.

  • #[MunitTools::lessThan(20)

  • #[MunitTools::lessThan(|2017-08-09|)]

Checks that the expression is less than or equal to the specified value.

  • #[MunitTools::lessThanOrEqualTo(20)]

  • #[MunitTools::lessThanOrEqualTo(|2017-08-09|)]

Checks that the expression is close to the first number, using the second number as a delta value.
In other words, checks that the expression belongs to the range defined by the first number +/- the second number.

  • #[MunitTools::closeTo(1, 0.01)]

Checks that the expression is equal to a specific value.
This matcher also accepts Dataweave objects.

  • #[MunitTools::equalTo('example')]

  • #[MunitTools::equalTo({example1: 1 , example2: 2}]

Iterable and Maps Matchers

You can use these matchers when your expression is either an Array or a Map of data.

Some of these matchers evaluate keys and values in the expression.
For example, you can evaluate:

  • Whether the map/array in the expression is empty.

  • Whether the map/array in the expression has a specific key.

You can also use some of these matchers to iterate inside the expression and evaluate each key and value using any other matcher.
Some examples are:

  • Whether every item in the map/array from the expression are greater than or less than a specific number.

  • Whether any item in the map/array from the expression starts or ends with a specific String.

These matchers are:

Matcher Description Example

Checks that every element in the expression matches the specified matcher.

This matcher only works for Arrays.

  • #[MunitTools::everyItem(MunitTools::notNullValue())]

  • #[MunitTools::everyItem(MunitTools::startsWith('a'))]

Checks that any element in the expression matches the specified matcher.

This matcher only works for Arrays.

  • #[MunitTools::hasItem(MunitTools::notNullValue())]

  • #[MunitTools::hasItem(MunitTools::startsWith('a'))]

Checks that the size of the expression matches the specified matcher.

  • #[MunitTools::hasSize(MunitTools::equalTo(5))]

  • #[MunitTools::hasSize(MunitTools::startsWith('a'))]

Checks that the expression is an empty collection.

  • #[MunitTools::isEmpty()]

Checks that the expression has a key that matches the specified matcher.

This matcher only works for Maps.

  • #[MunitTools::hasKey(MunitTools::equalTo('myKey'))]

  • #[MunitTools::hasKey(MunitTools::startsWith('a'))]

Checks that the expression has a value that matches the specified matcher.

This matcher only works for Maps.

  • #[MunitTools::hasValue(MunitTools::equalTo('myValue')]

  • #[MunitTools::hasValue(MunitTools::startsWith('a'))]