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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Testing Business Programs Without Constructing Domain Objects
  • Why Standard Test Automation Misses the Failures That Matter in AI Agent Systems
  • More Tests, More Confidence? Test Suites Are Investment Portfolios
  • One of Waterfall's Most Resilient Artifacts

Trending

  • RAG Architecture Patterns: Proxy, Caching, and Routing
  • The Permission Was Valid. The Action Was Not Authorized.
  • Spring AI on Local LLMs
  • Why Kotlin or Java Is the Wrong Question For Enterprises

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Jul. 12, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
53.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Testing Business Programs Without Constructing Domain Objects
  • Why Standard Test Automation Misses the Failures That Matter in AI Agent Systems
  • More Tests, More Confidence? Test Suites Are Investment Portfolios
  • One of Waterfall's Most Resilient Artifacts

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook