DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • DataWeave: Play With Dates (Part 1)
  • Implementing NetSuite Add Records Operation With MuleSoft - Part II
  • API-Led Example: MuleSoft
  • Tired of Messy Code? Master the Art of Writing Clean Codebases

Trending

  • How to Format Articles for DZone
  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • The Role of Functional Programming in Modern Software Development
  • SQL Server Index Optimization Strategies: Best Practices with Ola Hallengren’s Scripts
  1. DZone
  2. Data Engineering
  3. Data
  4. Implementing MUnit And MUnit Matchers With MuleSoft

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.

By 
Jitendra Bafna user avatar
Jitendra Bafna
DZone Core CORE ·
Updated Feb. 10, 20 · Analysis
Likes (4)
Comment
Save
Tweet
Share
23.1K Views

Join the DZone community and get the full member experience.

Join For Free

What 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().

openbanking

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.

Assert that


Code

XML
 




x
21


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<mule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
    xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:munit-tools="http://www.mulesoft.org/schema/mule/munit-tools"
4
    xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
5
    xsi:schemaLocation="
6
        http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
7
        http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
8
        http://www.mulesoft.org/schema/mule/munit-tools  http://www.mulesoft.org/schema/mule/munit-tools/current/mule-munit-tools.xsd">
9
    <munit:config name="openbanking-accounts-api-test-suite.xml" ></munit:config>
10
    <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">
11
        <munit:execution >
12
            <munit:set-event doc:name="Set Event" doc:id="a628818c-04dd-48ee-aa85-a7f20b9610fa" >
13
                <munit:attributes value="#[{uriParams:{accountId:'22289'}}]" ></munit:attributes>
14
            </munit:set-event>
15
            <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>
16
        </munit:execution>
17
        <munit:validation >
18
            <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>
19
        </munit:validation>
20
    </munit:test>
21
</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').

Assert that

Code

XML
 




xxxxxxxxxx
1
21


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<mule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
    xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:munit-tools="http://www.mulesoft.org/schema/mule/munit-tools"
4
    xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
5
    xsi:schemaLocation="
6
        http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
7
        http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
8
        http://www.mulesoft.org/schema/mule/munit-tools  http://www.mulesoft.org/schema/mule/munit-tools/current/mule-munit-tools.xsd">
9
    <munit:config name="openbanking-accounts-api-test-suite.xml" ></munit:config>
10
    <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">
11
        <munit:execution >
12
            <munit:set-event doc:name="Set Event" doc:id="a628818c-04dd-48ee-aa85-a7f20b9610fa" >
13
                <munit:attributes value="#[{uriParams:{accountId:'22289'}}]" ></munit:attributes>
14
            </munit:set-event>
15
            <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>
16
        </munit:execution>
17
        <munit:validation >
18
            <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>
19
        </munit:validation>
20
    </munit:test>
21
</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

XML
 




xxxxxxxxxx
1
21


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<mule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
    xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:munit-tools="http://www.mulesoft.org/schema/mule/munit-tools"
4
    xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
5
    xsi:schemaLocation="
6
        http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
7
        http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
8
        http://www.mulesoft.org/schema/mule/munit-tools  http://www.mulesoft.org/schema/mule/munit-tools/current/mule-munit-tools.xsd">
9
    <munit:config name="openbanking-accounts-api-test-suite.xml" ></munit:config>
10
    <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">
11
        <munit:execution >
12
            <munit:set-event doc:name="Set Event" doc:id="a628818c-04dd-48ee-aa85-a7f20b9610fa" >
13
                <munit:attributes value="#[{uriParams:{accountId:'22289'}}]" ></munit:attributes>
14
            </munit:set-event>
15
            <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>
16
        </munit:execution>
17
        <munit:validation >
18
            <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>
19
        </munit:validation>
20
    </munit:test>
21
</mule>



POM Munit Dependency and Plugin

Maven Dependency

XML  




xxxxxxxxxx
1
13


 
1
<dependency>
2
            <groupId>com.mulesoft.munit</groupId>
3
            <artifactId>munit-runner</artifactId>
4
            <version>2.2.1</version>
5
            <classifier>mule-plugin</classifier>
6
            <scope>test</scope>
7
        </dependency>   <dependency>
8
            <groupId>com.mulesoft.munit</groupId>
9
            <artifactId>munit-tools</artifactId>
10
            <version>2.2.1</version>
11
            <classifier>mule-plugin</classifier>
12
            <scope>test</scope>
13
</dependency>



Maven Plugin

XML
 




xxxxxxxxxx
1
23


 
1
<p>
2
                <groupId>com.mulesoft.munit.tools</groupId>
3
                <artifactId>munit-maven-plugin</artifactId>
4
                <version>${munit.version}</version>
5
                <executions>
6
                    <execution>
7
                        <id>test</id>
8
                        <p>test</phase>
9
                        <goals>
10
                            <goal>test</goal>
11
                            <goal>coverage-report</goal>
12
                        </goals>
13
                    </execution>
14
                </executions>
15
                <configuration>
16
                    <coverage>
17
                        <runCoverage>true</runCoverage>
18
                        <formats>
19
                            <format>html</format>
20
                        </formats>
21
                    </coverage>
22
                </configuration>
23
 </plugin>



YouTube Videos References



This is how you can implement the MUnit test cases for your MuleSoft application.


Further Reading

Testing Dataweave With MUnit

MUnit Testing With Mule 4

MuleSoft unit test Strings Data Types

Opinions expressed by DZone contributors are their own.

Related

  • DataWeave: Play With Dates (Part 1)
  • Implementing NetSuite Add Records Operation With MuleSoft - Part II
  • API-Led Example: MuleSoft
  • Tired of Messy Code? Master the Art of Writing Clean Codebases

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!