MUnit Matchers and Types (Mule 4)

Mulesoft

MUnit Matchers

MUnit matches are a set of DataWeave functions to define assertion conditions for any value in an expression. When defining matches, include the prefix MunitTools:: in the expression.

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

Type of Matchers

  • Core Matchers
  • String Matchers
  • Comparable Matchers
  • Iterable and Map Matchers

MUnit Core Matchers
MatchersDescriptionExample
nullValue()Checks that the expression is null.#[MunitTools::nullValue()]
notNullValue()Checks that the expression is not null.#[MunitTools::notNullValue()]

withMediaType(String)
Checks that the expression’s media type is the one specified.#[MunitTools::withMediaType(‘text/xml’)]
withEncoding(String)Checks that the expression’s encoding is the one specified.#[MunitTools::withEncoding(‘UTF-8’)]
both(Matcher, Matcher)Checks that both provided matchers are successful.#[MunitTools::both(MunitTools::notNullValue(),MunitTools::equalTo(‘example’))]

either(Matcher,Matcher)
Checks that at least one of the matchers is successful.#[MunitTools::either(MunitTools::nullValue(),MunitTools::equalTo(0))]

not(Object)
Checks if the provided matcher is not successful.#[MunitTools::not(0)]
anyOf(Array<Matcher>)Checks if any of the matchers are successful.#[MunitTools::anyOf([MunitTools::notNullValue(),MunitTools::withMediaType(‘text/xml’),MunitTools::isEmptyString()])]
allOf(Array<Matcher>)Checks if all of the matchers are successful.#[MunitTools::allOf([MunitTools::notNullValue(),MunitTools::withMediaType(‘text/xml’),MunitTools::isEmptyString()])]
MUnit String Matchers
MatchersDescRiptionExample
containsString(String)Checks that the expression contains the specified String.#[MunitTools::containsString(‘example’)]
startsWith(String)Checks that the expression starts with the specified String.#[MunitTools::startsWith(‘exam’)]
endsWith(String)Checks that the expression ends with the specified String.#[MunitTools::endsWith(‘ple’)]

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

equalToIgnoringCase(String)
Checks that the expression is equal to the specified String, ignoring case.#[MunitTools::equalToIgnoringCase(‘example’)]

equalToIgnoringWhiteSpace(String)
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’)]
stringContainsInOrder(Array<String>)Checks that the expression contains all of the specified substrings, regardless of the order of their appearance.#[MunitTools::stringContainsInOrder([‘an’, ‘example’])]

MUnit Comparable Matchers

Matchersdescriptionexample
greaterThan(Comparable)Checks that the expression is greater than the specified value.#[MunitTools::greaterThan(|2017-08-09|)]

greaterThanOrEqualTo(Comparable)
Checks that the expression is greater than or equal to the specified value.#[MunitTools::greaterThanOrEqualTo(20)]
lessThan(Comparable)Checks that the expression is less than to the specified value.#[MunitTools::lessThanOrEqualTo(20)]

closeTo(Number, Number)
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)]
equalTo(Object)Checks that the expression is equal to a specific value.#[MunitTools::equalTo(0)]

MUnit Map and Iterable Matchers

matchersdescriptionexample
everyItem(Matcher)Checks that every element in the expression matches the specified matcher.#[MunitTools::everyItem(MunitTools::notNullValue())]

hasItem(Object)
Checks that any element in the expression matches the specified matcher.#[MunitTools::hasItem(‘example’)]
hasSize(Matcher|Integer)Checks that the size of the expression matches the specified matcher.#[MunitTools::hasSize(MUnitTools::greaterThan(2))]

isEmpty()
Checks that the expression is an empty collection.#[MunitTools::isEmpty()]
hasKey(Matcher|String)Checks that the expression has a key that matches the specified matcher.#[MunitTools::hasKey(MunitTools::startsWith(‘a’))]
hasValue(Object)Checks that the expression has a value that matches the specified matcher.#[MunitTools::hasValue(MunitTools::startsWith(‘a’))]

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Leave a Reply

Your email address will not be published. Required fields are marked *