Contact Us 1-800-596-4880

Migrating Test Structure (MUnit)

This version of Mule reached its End of Life on May 2, 2023, when Extended Support ended.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

The MUnit test is divided into three scopes. All of them are optional:

Scope Description

Behavior

The behavior scope (<munit:behavior/>) is intended for setting the preconditions before executing the test logic.
Mocks and Spies go in this section.

Execution

The execution scope (<munit:execution/>) is meant to have the testing logic which will wait for all processes to finish before executing the next scope.

Validation

The validation scope (<munit:validation/>) is meant to have all the validations regarding the result of the execution scope.
Assertions and Verifications go in the validation scope.

The following examples compare MUnit tests in 1.x to 2.x.

Test in MUnit 1.x
<munit:test name="sampleTest" description="Test">
    <mock:when messageProcessor="mule:set-payload">
        <mock:then-return payload="#['Sample']"/>
    </mock:when>
    <flow-ref name="sampleFlow"/>
    <munit:assert-payload-equals expectedValue="#['SampleString']"/>
</munit:test>
Test in MUnit 2.x
<munit:test name="sampleTest" description="Test">
    <munit:behavior>
        <munit-tools:mock-when processor="mule:set-payload">
            <munit-tools:then-return>
                <munit-tools:payload value="#['Sample']" />
            </munit-tools:then-return>
        </munit-tools:mock-when>
    </munit:behavior>
    <munit:execution>
        <flow-ref name="sampleFlow"/>
    </munit:execution>
    <munit:validation>
        <munit-tools:assert-that expression="#[payload]" is="#[MunitTools::equalTo('SampleString')]"/>
    </munit:validation>
</munit:test>