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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • Batching Strategies: Throughput vs Latency in Practice
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • How to Migrate From C/C++ to Rust: A Practical Guide for Embedded IoT Systems

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

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
·
Updated Jan. 04, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
13.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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":"[email protected]"

}

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook