Unit Testing with MUnit
MUnit is MuleSoft's native testing framework — tests are XML-based flows that mock connectors and assert on payloads.
An MUnit test suite is an XML file in `src/test/munit/`. Each test case is an `<munit:test>` element containing behaviour mocks, the flow to execute, and assertions.
Mock the behaviour of connectors using `<munit-tools:mock-when>` to return controlled responses, keeping tests fast and deterministic without needing real database or HTTP connections.
The `<munit-tools:assert-that>` element supports a rich set of matchers: `equalTo`, `containsString`, `hasSize`, `isNull`, etc. Always assert both the payload shape and the HTTP status variable.
<munit:test name="get-orders-flow-success-test"
description="Should return orders when DB returns results">
<munit:behavior>
<munit-tools:mock-when processor="db:select">
<munit-tools:with-attributes>
<munit-tools:with-attribute attributeName="doc:name" whereValue="Query Orders" />
</munit-tools:with-attributes>
<munit-tools:then-return>
<munit-tools:payload value="#[[{ORDER_ID: '1', CUSTOMER_NAME: 'Alice', TOTAL_AMOUNT: 99.99}]]" />
</munit-tools:then-return>
</munit-tools:mock-when>
</munit:behavior>
<munit:execution>
<flow-ref name="get-orders-flow" />
</munit:execution>
<munit:validation>
<munit-tools:assert-that expression="#[sizeOf(payload)]"
is="#[MunitTools::equalTo(1)]" />
<munit-tools:assert-that expression="#[payload[0].customer]"
is="#[MunitTools::equalTo('Alice')]" />
<munit-tools:assert-that expression="#[vars.httpStatus]"
is="#[MunitTools::equalTo('200')]" />
</munit:validation>
</munit:test>