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:

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.

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.

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.

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