DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Streamlining Event Data in Event-Driven Ansible
  • Dynamic Web Forms In React For Enterprise Platforms
  • Unlocking Oracle 23 AI's JSON Relational Duality
  • Loading XML into MongoDB

Trending

  • Proactive Security in Distributed Systems: A Developer’s Approach
  • How to Introduce a New API Quickly Using Micronaut
  • How to Convert XLS to XLSX in Java
  • The Smart Way to Talk to Your Database: Why Hybrid API + NL2SQL Wins
  1. DZone
  2. Coding
  3. Languages
  4. MUnit JSON Assertion

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.

By 
Deepak Kushwaha user avatar
Deepak Kushwaha
·
Dec. 28, 17 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
15.3K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Objective

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

Steps

  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:

    <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
---
{
"test":{
"data":{
"name":"dk",
"age":"26"
}
}
}]]></dw:set-payload>
        </dw:transform-message>
    </flow>

 

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:beans>
        <spring:import resource="classpath:testbatch.xml"/>
    </spring:beans>
    <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>
        </scripting:transformer>
    </munit:test>
</mule>


Expected outcome file:

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


JSON Assertion (software development)

Opinions expressed by DZone contributors are their own.

Related

  • Streamlining Event Data in Event-Driven Ansible
  • Dynamic Web Forms In React For Enterprise Platforms
  • Unlocking Oracle 23 AI's JSON Relational Duality
  • Loading XML into MongoDB

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!