Studio Visual Editor
XML or Standalone Editor
<flow name="exampleFlow">
<set-variable variableName="#['my_variable']" value="#['some_string']" doc:name="Variable"/>
</flow>
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.
Matcher Name | Description |
---|---|
|
Matches if the evaluated object is |
|
Matches if the evaluated object is not |
|
Matches if the evaluated object is an |
|
Matches if the evaluated object is a |
|
Matches if the evaluated object is a |
|
Matches if the evaluated object is a |
|
Matches if the evaluated object is an |
|
Matches if the evaluated object is a |
|
Matches if the evaluated object is a |
|
Matches if the evaluated object is a |
|
Matches if the evaluated object is a |
|
Matches if the evaluated object is a |
|
Matches if the evaluated object is a |
|
Matches if the evaluated object is a |
|
Matches if the evaluated object is a |
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.
Matchers Name | Description |
---|---|
|
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. |
For the purposes of this document, we assume we are testing the following Mule code:
<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. |
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
.
<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.
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:
<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>
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
.
<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>
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.
<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).
If you wish to run assertions over Mule message properties and use MUnit matchers, you can use the valueOf()
function. For instance:
<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 |
---|---|
|
Returns an ElementMatcher for the |
|
Returns an ElementMatcher for the |
|
Returns an ElementMatcher for the |
|
Returns an ElementMatcher for the |
|
Returns an ElementMatcher for the |
With these functions you could change the example above to:
<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>