Over a million developers have joined DZone.

MUnit JSON Assertion

DZone's Guide to

MUnit JSON Assertion

Learn how to write MUnit tests to test JSON response payloads from a REST API in Mule, using a Groovy script, to avoid contract conflicts.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

For a REST API in Mule which returns a JSON response, it is always advisable to write MUnit tests and assert the entire JSON response payload to avoid contract conflicts/mismatches. MUnit does not have any out-of-the-box component to compare two JSON files. In this example, we are using a Groovy script for this comparison/assertion.


Our objective is loading expected JSON data from a file and comparing it with actual MUnit payload which we got from the tested flow.


  1. Create a Mule project using Anypoint Studio.

  2. Create a flow called Flow1 with an HTTP inbound endpoint and set a JSON payload to send back as a response.

  3. Create an MUnit test case for the above flow.

  4. Inside test/resources, create a JSON file (expectedOutcome.json) which holds the expected JSON payload.

  5. In your MUnit TestCase, add a flow ref to call Flow1.

  6. After the flow ref, create a variable and load the expected json data from the file using the following MEL: #[getResource('expectedOutcome.json').asString()] 

  7. Now, in your MUnit test case, drag and drop a Groovy component after the flow-ref.

  8. Add the following script to the Groovy component: def map1 = new groovy.json.JsonSlurper().parseText(payload)def map2 = new groovy.json.JsonSlurper().parseText(flowVars.expectedOutcome)assert map1 == map2

  9. Run your MUnit test; it will return a Test Case success if both JSON files/data are identical.

Refer to the code below for the full Flow and MUnit code.


    <flow name="Flow1">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json


MUnit Test Case:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd">
    <munit:config name="munit" doc:name="MUnit configuration" mock-connectors="false" mock-inbounds="false"/>
        <spring:import resource="classpath:testbatch.xml"/>
    <munit:test name="testbatch-test-suite-Flow1Test" description="Test">
        <flow-ref name="Flow1" doc:name="Flow-ref to Flow1"/>
        <set-variable variableName="expectedOutcome " value="#[getResource('expectedOutcome.json').asString()]" doc:name="set expectedOutcome Variable"/>
        <scripting:transformer doc:name="Groovy">
            <scripting:script engine="Groovy"><![CDATA[def map1 = new groovy.json.JsonSlurper().parseText(payload)
def map2 = new groovy.json.JsonSlurper().parseText(flowVars.expectedOutcome)

assert map1 == map2]]></scripting:script>

Expected outcome file:

  "test": {
    "data": {
      "name": "dk",
      "age": "26"

With SnapLogic’s integration platform you can save millions of dollars, increase integrator productivity by 5X, and reduce integration time to value by 90%. Sign up for our risk-free 30-day trial!

munit ,mule 3.7 ,unit testing ,groovy ,json ,integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}