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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. MUnit Testing With Mule 4

MUnit Testing With Mule 4

In this article, see how to create and run an MUnit test.

Kuldeep Rana user avatar by
Kuldeep Rana
·
Jul. 12, 19 · Tutorial
Like (2)
Save
Tweet
Share
44.79K Views

Join the DZone community and get the full member experience.

Join For Free

MUnit is a Mule Application Testing Framework that allows you to easily build automated tests for your Integration and APIs. It provides a full suite of integration and unit test capabilities, and it is fully integrated with Maven.

MUnit is fully integrated with Anypoint Studio and allows you to create, design, and test your MUnit tests just like you would Mule Applications.

With MUnit, you can:

  • Create your test by writing Mule code

  • Enable or ignore a particular test

  • Check visual coverage in the studio

  • Generate coverage reports

In this article, I'll show you how to create and run an MUnit test. I am performing this example in Mule 4. MUnit version 2.0, works with all mule version since 4.0

How to Create MUnit Test for Mule Flow

Create a project in Anypoint Studio. The flow looks like, as shown below:

Image title

Code:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="89ac8a6f-ce64-4726-927f-679b06e56aec" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="munitFlow" doc:id="24f8fe4d-2476-4112-9188-8ebc81458030" >
<http:listener doc:name="Listener" doc:id="986cdf76-9129-4aa9-8f36-a5bde486398c" config-ref="HTTP_Listener_config" path="/test"/>
<set-variable value="test" doc:name="Set Variable" doc:id="276e0ebb-2711-4725-a548-78dada3f0f1b" variableName="code"/>
<set-payload value="#[vars.code]" doc:name="Set Payload" doc:id="c48a4a9b-0b0f-4924-925e-40cbaa97d744" />
<logger level="INFO" doc:name="Logger" doc:id="bd82f51a-9c2e-4652-992d-599a35ae3c86" message="#[payload]"/>
</flow>
</mule>
  • Right-click on the flow created. Select MUnit -> Create new MUnit.xml suite.

Image title

This will create an MUnit test suite, which will be present in src/test/munit.

Search for the assert that in the mule palette. Drag and drop it in the validation section of the munit-test-suite flow.

In the properties section of assert that, set the values for the EXPRESSION, IS, and MESSAGE.

Image title

Code:

<?xml version="1.0" encoding="UTF-8"?>

<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="munit-test-suite.xml" />
<munit:test name="munit-test-suite-munitFlowTest" description="Test" doc:id="77a653f8-0adf-47e1-84db-6b0914db7cd7" >
<munit:execution >
<flow-ref doc:name="Flow-ref to munitFlow" doc:id="c163902a-d303-4fba-a75b-15486d590553" name="munitFlow"/>
</munit:execution>
<munit:validation >
<munit-tools:assert-that doc:name="Assert that" doc:id="b5c1f3e3-e06f-41ba-a253-9ac09fb0ebd9" expression="#[payload]" is="#[MunitTools::notNullValue()]"/>
</munit:validation>
</munit:test>
</mule>

Run the MUnit Test

  • Right-click on flow or canvas of MUnit test flow.   Select Run MUnit Suite.

  • After runningMUnit suite, it will provide test result, errors, failures (if any) and other details on the console in Anypoint Studio.

Image title

Once the test is complete, you will get a green color horizontal line of the left side of the studio below the package explorer. This means that your test case has been complete, successfully executed, and passed.

                                            RUN: 1/1                      ERRORS: 0             FAILURE: 1

unit test

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Bye Bye, Regular Dev [Comic]
  • Better Performance and Security by Monitoring Logs, Metrics, and More
  • ChatGPT Prompts for Agile Practitioners
  • Differences Between Site Reliability Engineer vs. Software Engineer vs. Cloud Engineer vs. DevOps Engineer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: