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
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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Trending

  • How to Practice TDD With Kotlin
  • Java Virtual Threads and Scaling
  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • A Modern Stack for Building Scalable Systems

Testing Dataweave With MUnit

In this post, we take a look at how to configure and test your Dataweave app using Mulsesoft's MUnit tool. Read on for more detail!

By 
Satheesh Kumar user avatar
Satheesh Kumar
·
Updated Jan. 04, 18 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
13.2K Views

Join the DZone community and get the full member experience.

Join For Free

Its easier to test data weave when developing the code using the previewer, but when you are moving the code into CI/CD we need an automated testing where Munit comes into the picture. Most developers use an entire flow to test Dataweave, but this is not the recommended way.

Many developers are not using one of the best-hidden features of Dataweave, which is referring a dwl file from resources.

The advantages of using a separate dwl file are:

  1. Cleaner XML files.

  2. Reusability.

  3. Easy testing.

In this article, I am going to show you how to test Dataweave alone, using a simple example.

{
 "Name":"satheesh",
 "Role":"Engineer",
 "ID":"12343534"
  }

This JSON needs to be converted to

{
 "Name":"satheesh",
  "ID":"12343534",
 "Email":"sath1234@abc.com"

}

The Dataweave code for this should look like what I've got below:

%dw 1.0
%output application/json
---
{
Name: payload.Name,
ID: payload.ID,
Email: payload.Name[0..3] ++ payload.ID[0..3] ++ "@abc.com"
}

Now let create a new dwl file under src/main/resources/mapping

Image title


Add the Dataweave code in the file and refer to the file in the transform message, as shown below.

Image title

Now your config.xml will look cleaner and the Dataweave file can be used in other transformations too.

 <flow name="sftp-testFlow">
        <sftp:inbound-endpoint connector-ref="SFTP" host="xxxxx" port="22" path="xxxxxxx" user="santonyraj" password="yyyyyyy" responseTimeout="10000" doc:name="SFTP"/>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload resource="classpath:mapping/sample.dwl"/>
        </dw:transform-message>
    </flow>

Now let's see how to test the file using Munit:

  1. Create an MUnit test suite.

  2. Use a set Message to set your payload.

  3. Use a transform message and refer your dwl file (Don't use UI to refer the file as it will overwrite the original one. Instead, use the xml and add <dw:set-payload resource="classpath:mapping/sample.dwl"/> in the transform message).

  4. Use assert payload to check your output.

Image title

The config.xml should look like this:

    <munit:test name="dataweave-test" description="Test">
        <munit:set payload="#[getResource('input.json').asString()]" doc:name="Set Message"/>
        <dw:transform-message doc:name="Transform Message">
        <dw:set-payload resource="classpath:mapping/sample.dwl"/>
        </dw:transform-message>
        <munit:assert-payload-equals expectedValue="#[getResource('output.json').asString()]" doc:name="Assert Payload"/>
    </munit:test>

Image title

In this way, all the mapping can be tested.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

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: