Implementing MUnit And MUnit Matchers With MuleSoft
MUnit is a Mule application testing framework that allows you to build automated tests for your Mule integrations and APIs.
Join the DZone community and get the full member experience.
Join For FreeWhat Is the MUnit Framework?
MUnit is a Mule application testing framework that allows you to build automated tests for your Mule integrations and APIs. MUnit is very well-integrated with Anypoint Studio and Maven.
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
Matchers | Description | Example |
---|---|---|
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
Matchers | DescRiption | Example |
---|---|---|
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
Matchers | description | example |
---|---|---|
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
matchers | description | example |
---|---|---|
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'))] |
Now we will see some examples related to Core and String Matchers.
Check Response Payload Is Not Null
We will write MUnit to check the payload is not null and we will use core matcher notNullValue().
In the above MUnit test case, we are setting up URI Param in Set Event and we will use Assert that to check if Payload is not null.
In the below screenshot, we are setting up Set Event to pass URI Parameter to flow and Matcher notNullValue() to Assert that.
Code
<mule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:munit-tools="http://www.mulesoft.org/schema/mule/munit-tools"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
http://www.mulesoft.org/schema/mule/munit-tools http://www.mulesoft.org/schema/mule/munit-tools/current/mule-munit-tools.xsd">
<munit:config name="openbanking-accounts-api-test-suite.xml" ></munit:config>
<munit:test name="openbanking-accounts-api-test-suite-get:\accounts\(accountId)\balances:openbanking-accounts-api-configTest" doc:id="a217f12d-7375-4e35-81ae-a3e75908224e" description="Test">
<munit:execution >
<munit:set-event doc:name="Set Event" doc:id="a628818c-04dd-48ee-aa85-a7f20b9610fa" >
<munit:attributes value="#[{uriParams:{accountId:'22289'}}]" ></munit:attributes>
</munit:set-event>
<flow-ref doc:name="Flow-ref to get:\accounts\(accountId)\balances:openbanking-accounts-api-config" doc:id="b18bed3e-21e8-4869-a36f-39e0614a43d9" name="get:\accounts\(accountId)\balances:openbanking-accounts-api-config"></flow>
</munit:execution>
<munit:validation >
<munit-tools:assert-that doc:name="Assert that" doc:id="cf16931a-1697-4e52-be1b-b7919c77f157" expression="#[payload]" is="#[MunitTools::notNullValue()]" message="Payload is empty"></munit>
</munit:validation>
</munit:test>
</mule>
Check Response Payload MediaType Is Application/JSON
We will write MUnit to check the response payload media type is application/JSON and we will use core matcher withMediaType('application/JSON').
Code
xxxxxxxxxx
<mule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:munit-tools="http://www.mulesoft.org/schema/mule/munit-tools"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
http://www.mulesoft.org/schema/mule/munit-tools http://www.mulesoft.org/schema/mule/munit-tools/current/mule-munit-tools.xsd">
<munit:config name="openbanking-accounts-api-test-suite.xml" ></munit:config>
<munit:test name="openbanking-accounts-api-test-suite-get:\accounts\(accountId)\balances:openbanking-accounts-api-configTest" doc:id="a217f12d-7375-4e35-81ae-a3e75908224e" description="Test">
<munit:execution >
<munit:set-event doc:name="Set Event" doc:id="a628818c-04dd-48ee-aa85-a7f20b9610fa" >
<munit:attributes value="#[{uriParams:{accountId:'22289'}}]" ></munit:attributes>
</munit:set-event>
<flow-ref doc:name="Flow-ref to get:\accounts\(accountId)\balances:openbanking-accounts-api-config" doc:id="b18bed3e-21e8-4869-a36f-39e0614a43d9" name="get:\accounts\(accountId)\balances:openbanking-accounts-api-config"></flow>
</munit:execution>
<munit:validation >
<munit-tools:assert-that doc:name="Assert that" doc:id="cf16931a-1697-4e52-be1b-b7919c77f157" expression="#[payload]" is="#[MunitTools::withMediaType('application/json')]" message="Payload is not json"></munit>
</munit:validation>
</munit:test>
</mule>
Check Response Payload Field AccountId Start With 228
We will write MUnit to check the response payload field AccountId Start With 228 and we will use a string matcher startsWith('228').
Code
xxxxxxxxxx
<mule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:munit-tools="http://www.mulesoft.org/schema/mule/munit-tools"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
http://www.mulesoft.org/schema/mule/munit-tools http://www.mulesoft.org/schema/mule/munit-tools/current/mule-munit-tools.xsd">
<munit:config name="openbanking-accounts-api-test-suite.xml" ></munit:config>
<munit:test name="openbanking-accounts-api-test-suite-get:\accounts\(accountId)\balances:openbanking-accounts-api-configTest" doc:id="a217f12d-7375-4e35-81ae-a3e75908224e" description="Test">
<munit:execution >
<munit:set-event doc:name="Set Event" doc:id="a628818c-04dd-48ee-aa85-a7f20b9610fa" >
<munit:attributes value="#[{uriParams:{accountId:'22289'}}]" ></munit:attributes>
</munit:set-event>
<flow-ref doc:name="Flow-ref to get:\accounts\(accountId)\balances:openbanking-accounts-api-config" doc:id="b18bed3e-21e8-4869-a36f-39e0614a43d9" name="get:\accounts\(accountId)\balances:openbanking-accounts-api-config"></flow>
</munit:execution>
<munit:validation >
<munit-tools:assert-that doc:name="Assert that" doc:id="cf16931a-1697-4e52-be1b-b7919c77f157" expression="#[payload]" is="#[MunitTools::startsWith('228')]" message="Payload is not json"></munit>
</munit:validation>
</munit:test>
</mule>
POM Munit Dependency and Plugin
Maven Dependency
xxxxxxxxxx
<dependency>
<groupId>com.mulesoft.munit</groupId>
<artifactId>munit-runner</artifactId>
<version>2.2.1</version>
<classifier>mule-plugin</classifier>
<scope>test</scope>
</dependency> <dependency>
<groupId>com.mulesoft.munit</groupId>
<artifactId>munit-tools</artifactId>
<version>2.2.1</version>
<classifier>mule-plugin</classifier>
<scope>test</scope>
</dependency>
Maven Plugin
xxxxxxxxxx
<p>
<groupId>com.mulesoft.munit.tools</groupId>
<artifactId>munit-maven-plugin</artifactId>
<version>${munit.version}</version>
<executions>
<execution>
<id>test</id>
<p>test</phase>
<goals>
<goal>test</goal>
<goal>coverage-report</goal>
</goals>
</execution>
</executions>
<configuration>
<coverage>
<runCoverage>true</runCoverage>
<formats>
<format>html</format>
</formats>
</coverage>
</configuration>
</plugin>
YouTube Videos References
This is how you can implement the MUnit test cases for your MuleSoft application.
Further Reading
Opinions expressed by DZone contributors are their own.
Comments