Categories: Mule 4Mulesoft

MUnit Matchers and Types (Mule 4)

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

Recent Posts

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago

Binary Search Tree (BST)

A Binary Search Tree (BST) is a type of binary tree that satisfies the following…

1 year ago