MUnit matchers are functions that help validate values in mocks and assertions during testing. They allow matching based on general types rather than specific values. Common matchers check for data types like null, strings, collections, and more. Additional matchers test regular expressions and containment. MUnit also provides matchers to directly access Mule message properties and attachments for assertions.
2. OVERVIEW
MUnit matchers are a set of MEL functions that help in the creation of mocks
and validations (verifications/assertions). They enable you to define mocks,
verifications and assertions in terms of general values rather than specific
hardcoded values.
A matcher is a boolean function. In most of the MUnit matchers, this function
is a class type comparison. Its purpose is to validate if an argument belongs
to a certain class type.
A matcher does not receive parameters; rather, it’s a parameter itself.
3. isNull()
• Matches if the evaluated object is Null.
isNotNull()
• Matches if the evaluated object is not Null.
4. anyObject()
• Matches if the evaluated object is an Object.
anyBoolean()
• Matches if the evaluated object is a Boolean.
10. …MATCHER FUNCTIONS CONTINUED
MUnit also offers some additional, rather specific, type of matchers. These
matchers evaluate thecontent of the parameter, not its type; even so, they do
not evaluate by specific values.
matchRegex(<regex>)
matchContains(<string_to_look_for>)
• Matches if the value of the attribute during the test run verifies the user-provided
regular expression
• Matches if the value of the attribute during the test run contains the specified user-
provided string.
11. USAGE INSTRUCTION
• MUnit matchers are always used in combination with the Mocks, Spy, Verify and
Assert message processors, but never on their own.
12. MUNIT MATCHERS OVER MULE
MESSAGE PROPERTIES
• If you wish to run assertions over Mule message properties and use MUnit matchers,
you can use the valueOf() function.
• To make it easier to write this type of assertions, MUnit offers a another set of MEL
functions that provide direct access for Mule message’s properties ElementMatcher.
messageInboundProperty(propertyName)
• Returns an ElementMatcher for the inbound property with the name
provided.