Contact Us 1-800-596-4880

MUnit Matchers

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.

The table bellow lists the available MUnit matchers.

Table 1. MUnit Matcher Functions
Matcher Name Description

isNull()

Matches if the evaluated object is Null.

isNotNull()

Matches if the evaluated object is not Null.

anyObject()

Matches if the evaluated object is an Object.

anyBoolean()

Matches if the evaluated object is a Boolean.

anyByte()

Matches if the evaluated object is a Byte.

anyByteArray().

Matches if the evaluated object is a ByteArray.

anyInt()

Matches if the evaluated object is an Integer.

anyDouble()

Matches if the evaluated object is a Double.

anyFloat()

Matches if the evaluated object is a Float.

anyShort()

Matches if the evaluated object is a Short.

anyString()

Matches if the evaluated object is a String.

anyCollection()

Matches if the evaluated object is a Collection.

anyList()

Matches if the evaluated object is a List.

anySet()

Matches if the evaluated object is a Set.

anyMap()

Matches if the evaluated object is a Map.

MUnit also offers some additional, rather specific, type of matchers. These matchers evaluate the content of the parameter, not its type; even so, they do not evaluate by specific values.

Table 2. MUnit Matcher Functions
Matchers Name Description

matchRegex(<regex>)

Matches if the value of the attribute during the test run verifies the user-provided regular expression.

matchContains(<string_to_look_for>)

Matches if the value of the attribute during the test run contains the specified user-provided string.

Applying MUnit Matchers

For the purposes of this document, we assume we are testing the following Mule code:

Studio Visual Editor

set variable munit matcher

XML or Standalone Editor

<flow name="exampleFlow">
  <set-variable variableName="#['my_variable']" value="#['some_string']" doc:name="Variable"/>
</flow>
MUnit matchers are always used in combination with the Mocks, Spy, Verify and Assert message processors, but never on their own.

Using MUnit Matchers in a Mock Definition

Used in combination with MUnit Matchers, the Mock message processor allows you to create mocks that match by information other than values, such as content type of a specific attribute. In the example below, the mock matches if and only if the content of the attribute value is of type String.

Studio Visual Editor

mock munit matchers

XML or Standalone Editor

<mock:when messageProcessor="mule:set-variable">
  <mock:with-attributes>
    <mock:with-attribute whereValue="#['my_variable']" name="variableName"/>
    <mock:with-attribute whereValue="#[anyString()]" name="value"/>
  </mock:with-attributes>
</mock:when>

In the above example, the mock definition is triggered when the code being tested reaches the set-variable message processor, if and only if the content of attribute value is of type String, disregarding the actual string value.

If not using a matcher, the only way to produce a match is to evaluate for the literal value. If the test code evolves and changes, chances are the contents of attribute value would also change, and your test would fail.

Using MUnit Matchers in a Verification Definition

Used in combination with MUnit Matchers, the Verify message processor allows you to verify when a message processor is called by matching information other than values, such as the content type of a specific attribute. In the example below, the verification is successful if and only if the set-variable message processor has set variable my_variable, of content type String. solve use cases like:

Studio Visual Editor

verify definition

XML or Standalone Editor

<mock:verify-call messageProcessor="mule:set-variable" times="1" doc:name="Verify Call">
  <mock:attributes>
    <mock:with-attribute whereValue="#['my_variable']" name="variableName"/>
    <mock:attribute whereValue="#[anyString()]" name="value"/>
  </mock:attributes>
</mock:verify-call>

Using MUnit Matchers in Spy Definition

Used in combination with MUnit Matchers, the Spy message processor allows you to spy message processors matched by information other than values, such as the content type of a specific attribute. In the example below, the message processor in the test code can be spied if and only if the set-variable message processor has set the variable my_variable, of content type String.

Studio Visual Editor

spy with assert match

XML or Standalone Editor

<mock:spy messageProcessor="mule:set-payload" doc:name="Spy">
  <mock:with-attributes>
    <mock:with-attribute whereValue="#['my_variable']" name="variableName"/>
    <mock:attribute whereValue="#[anyString()]" name="value"/>
  </mock:with-attributes>
  <mock:assertions-before-call>
    <!-- Define actions -->
  </mock:assertions-before-call>
  <mock:assertions-after-call>
    <!-- Define actions -->
  </mock:assertions-after-call>
</mock:spy>

Using MUnit Matchers in an Assertion Definition

You can greatly enhance the power of the Assert message processors when using them in conjunction with MUnit matchers, for example by validating that the content of a Mule message is of a specific type.
The example below validates that the content of a Mule message is a boolean.

Studio Visual Editor

assert anyboolean munit matchers

XML or Standalone Editor

<munit:test name="munit-test-suite-exampleFlowTest" description="Test">
    <flow-ref name="exampleFlow" doc:name="Flow-ref to exampleFlow"/>
    <munit:assert-true message="oops!" condition="#[valueOf(payload).is(anyBoolean())]" />
</munit:test>

Notice the content of the attribute condition:

#[
valueOf(payload)    (1)
.is(anyBoolean())   (2)
]
1 Creates an ElementMatcher for payload.
2 Run anyBoolean() MUnit matcher over the payload of the Mule message.

In this example we first need to create an ElementMatcher for the payload (1). Then, we can ask the ElementMatcher to run the anyBoolean() matcher over itself, by calling the method is() (2).

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. For instance:

Studio Visual Editor

assert variable isboolean munit matcher

XML or Standalone Editor

<munit:test name="munit-test-suite-exampleFlowTest" description="Test"> <flow-ref name="exampleFlow" doc:name="Flow-ref to exampleFlow"/> <munit:assert-true message="oops!" condition="#[valueOf(flowVars['my_variable']).is(anyBoolean())]" /> </munit:test>

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.

Function Description

messageInboundProperty(propertyName)

Returns an ElementMatcher for the inbound property with the name provided.

messageOutboundProperty(propertyName)

Returns an ElementMatcher for the outbound property with the name provided.

messageInvocationProperty(propertyName)

Returns an ElementMatcher for the invocation property with the name provided.

messageInboundAttachment(propertyName)

Returns an ElementMatcher for the inbound attachment with the name provided.

messageOutboundAttachment(propertyName)

Returns an ElementMatcher for the outbound attachment with the name provided.

With these functions you could change the example above to:

Studio Visual Editor

assert invocation property example

XML or Standalone Editor

<munit:test name="munit-test-suite-exampleFlowTest" description="Test">
    <flow-ref name="exampleFlow" doc:name="Flow-ref to exampleFlow"/>
    <munit:assert-true message="oops!" condition="#[messageInvocationProperty('my_variable').is(anyBoolean())]" doc:name="Assert True"/>
</munit:test>