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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

Related

  • Driving DevOps With Smart, Scalable Testing
  • Unit Testing Large Codebases: Principles, Practices, and C++ Examples
  • Practical Use of Weak Symbols
  • Generate Unit Tests With AI Using Ollama and Spring Boot

Trending

  • Memory Leak Due to Time-Taking finalize() Method
  • Integrating Model Context Protocol (MCP) With Microsoft Copilot Studio AI Agents
  • The Full-Stack Developer's Blind Spot: Why Data Cleansing Shouldn't Be an Afterthought
  • Metrics at a Glance for Production Clusters
  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.

By 
Kuldeep Rana user avatar
Kuldeep Rana
·
Jul. 12, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
52.1K 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.

Related

  • Driving DevOps With Smart, Scalable Testing
  • Unit Testing Large Codebases: Principles, Practices, and C++ Examples
  • Practical Use of Weak Symbols
  • Generate Unit Tests With AI Using Ollama and Spring Boot

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!