MUnit 2 - JSON exact match in Mule 4

I see unit tests to be returning contents that exactly match the intent. JsonUnit was very helpful when I was working with Mule 3 but this time I wanted to get the default matchers (MUnit 2) available in Mule 4 to work

After many trials, I got this setup to be the simplest to work with:

<munit:validation>
    <munit-tools:assert-that
        doc:name="Assert that payload matches expected structure"
        is="#[output application/json
                --- 
                MunitTools::equalTo(MunitTools::getResourceAsString('sample_data/databases/response.json'))]"
        message="Response does not match expected structure"
        expression="#[output application/json
                        ---
                        write(payload, 'application/json')]"/>
</munit:validation>

Another colleague suggested this more readable version:

<munit:validation>
    <set-variable
        variableName="expectedValue"
        value="#[MunitTools::getResourceAsString('sample_data/databases/response.json')]"
        doc:name="loadExpectedValue"
        mimeType="application/json"/>
    <munit-tools:assert-that
        expression="#[payload]"
        is="#[MunitTools::equalTo(vars.expectedValue)]"
        message="The response payload is not correct!"
        doc:name="Assert That - Payload is Expected"/>
</munit:validation>

In both approaches, the point to note is that mimeType to expect has to be informed to MUnit carefully. In my case, this is application/json

links

social