Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

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:

set-variable-munit-matcher


    
            
         
1
2
3
&lt;flow name="exampleFlow"&gt;
  &lt;set-variable variableName="#['my_variable']" value="#['some_string']" doc:name="Variable"/&gt;
&lt;/flow&gt;
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.

mock-munit-matchers


    
             
          
1
2
3
4
5
6
&lt;mock:when messageProcessor="mule:set-variable"&gt;
  &lt;mock:with-attributes&gt;
    &lt;mock:with-attribute whereValue="#['my_variable']" name="variableName"/&gt;
    &lt;mock:with-attribute whereValue="#[anyString()]" name="value"/&gt;
  &lt;/mock:with-attributes&gt;
&lt;/mock:when&gt;

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:

verify-definition


    
             
          
1
2
3
4
5
6
&lt;mock:verify-call messageProcessor="mule:set-variable" times="1" doc:name="Verify Call"&gt;
  &lt;mock:attributes&gt;
    &lt;mock:with-attribute whereValue="#['my_variable']" name="variableName"/&gt;
    &lt;mock:attribute whereValue="#[anyString()]" name="value"/&gt;
  &lt;/mock:attributes&gt;
&lt;/mock:verify-call&gt;

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.

spy-with-assert-match


    
             
          
1
2
3
4
5
6
7
8
9
10
11
12
&lt;mock:spy messageProcessor="mule:set-payload" doc:name="Spy"&gt;
  &lt;mock:with-attributes&gt;
    &lt;mock:with-attribute whereValue="#['my_variable']" name="variableName"/&gt;
    &lt;mock:attribute whereValue="#[anyString()]" name="value"/&gt;
  &lt;/mock:with-attributes&gt;
  &lt;mock:assertions-before-call&gt;
    &lt;!-- Define actions --&gt;
  &lt;/mock:assertions-before-call&gt;
  &lt;mock:assertions-after-call&gt;
    &lt;!-- Define actions --&gt;
  &lt;/mock:assertions-after-call&gt;
&lt;/mock:spy&gt;

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.

assert-anyboolean-munit-matchers

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

Notice the content of the attribute condition:


          
       
1
2
3
4
#[
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:

assert-variable-isboolean-munit-matcher

<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:

assert-invocation-property-example


    
              
           
1
2
3
4
&lt;munit:test name="munit-test-suite-exampleFlowTest" description="Test"&gt;
    &lt;flow-ref name="exampleFlow" doc:name="Flow-ref to exampleFlow"/&gt;
    &lt;munit:assert-true message="oops!" condition="#[messageInvocationProperty('my_variable').is(anyBoolean())]" doc:name="Assert True"/&gt;
&lt;/munit:test&gt;