Hear from Salesforce leaders on how to create and deploy Agentforce agents.
Contact Us 1-800-596-4880

Testing DataWeave Libraries and Modules

The DataWeave testing framework enables you to build automated unit and integration tests for your DataWeave libraries and mappings. You can run tests to evaluate .dwl files and compare them to the output produced by the system you’re testing. The framework provides a full suite of integration and unit test capabilities with a built-in set of assertion functions, and it is completely integrated with Maven and Surefire for your continuous deployment environment.

Use snapshot testing for your integration tests by storing the expected outputs as a separate file rather than as a string literal inside the test code.

You can create tests for your DataWeave mappings and modules either by using the Visual Studio Code (VS Code) plugin, see Installing and Using the DataWeave Extension for instructions, or manually by modifying your project’s pom.xml file.

Add the DataWeave Testing Framework to Your Project

To enable the DataWeave Testing Framework in your project:

  • Add the following Maven dependency to your project’s pom.xml file:

     <dependency>
       <groupId>org.mule.weave</groupId>
       <artifactId>data-weave-testing-framework</artifactId>
       <version>${data.weave.testing.framework.version}</version>
       <scope>test</scope>
     </dependency>
    xml

Tests Structure

The following table shows the different files that compose a test, a description of their functionalities, and their default locations:

File Description Location

DataWeave Mapping

The .dwl file that contains the mapping to test.

src/main/dw/<package>/<mappingFileName>

DataWeave Test

The .dwl file that contains the tests and assertions to execute.

src/test/dw/<package>/<testFileName>

Input

The file that contains the input to evaluate against the mapping. Depending on the test, this file is optional.

src/test/resources/<mappingPath>/<scenario>/inputs/<inputFile>

Output

The file that contains the output to check against the results of the mapping.

src/test/resources/<mappingPath>/<scenario>/<outputFile>

Create an Integration Test for a Mapping

To test a DataWeave mapping, create an integration test by following these steps:

  1. Create a .dwl file with a name ending in Test in the src/test/dw/<package>/<testFileName> folder.

  2. In the test file’s header, import the dw::test::Tests and dw::test::Asserts modules.

  3. In the test file’s body, nest any number of sections (“sectionName”), followed by describedBy, and specify the test name at the end (“testName”), followed by in do {<codeExecution/assertion>}.

  4. In the code execution and assertion part of the test file, specify the following:

    • The inputsFrom(<inputPath>) function, to get the inputs to evaluate against the mapping

    • The evalPath(<mapping/scenario>) function, to specify the mapping path

    • The must equalTo matcher to check the results against the expected output specified by function outputFrom(<mapping/scenario>)

  5. Add the expected inputs and outputs to the src/test/resources/<mappingPath> folder.

  6. If you want to configure the reader properties for the inputs, create a properties file with suffix {fileName}-config.properties in the src/test/resources/<mappingPath>/inputs/ folder.

Example Mapping Test

The following example shows a DataWeave test that contains a DataWeave mapping file, an input file, a reader properties configuration file, an output file, and a test file, respectively:

  • DataWeave mapping in src/main/dw/myPackage/MyMapping.dwl

    %dw 2.0
    output application/json
    ---
    {
      hello: "world"
    }
    dataweave
  • Input file in src/test/resources/myPackage/MyMapping/NewScenario/inputs/payload.json

    {
      "message": "Hello world!"
    }
    dataweave
  • Reader properties configuration file in src/test/resources/myPackage/MyMapping/NewScenario/inputs/payload-config.properties

    streaming=false
    dataweave
  • Expected output file in src/test/resources/myPackage/MyMapping/NewScenario/out.json

    {
      "hello": "world"
    }
    dataweave
  • Test file in src/test/dw/myPackage/MyMappingTest.dwl

    %dw 2.0
    import * from dw::test::Tests
    import * from dw::test::Asserts
    ---
    "Test MyMapping" describedBy [
       "Assert NewScenario" in do {
           evalPath("myPackage/MyMapping.dwl", inputsFrom("myPackage/MyMapping/NewScenario"),"application/json") must
                     equalTo(outputFrom("myPackage/MyMapping/NewScenario"))
       }
    ]
    dataweave

Create a Unit Test for a Module

To create a unit test for a module:

  1. Create a .dwl file with a name ending in Test in the src/test/dw/<package>/<testFileName> folder.

  2. In the test file’s header, import dw::test::Tests, dw::test::Asserts, and the custom module you want to test, for example, "MyModule".

  3. In the test file’s body, nest any number of sections (“sectionName”) followed by describedBy, and specify the test name at the end (“testName”).

  4. In the code execution and assertion part of the test, call the function you want to test (functionToTest()) and add an assertion against its result, for example, must beObject().

Example Unit Test for a Module

Example src/test/dw/MyModuleTest.dwl file:
%dw 2.0
import * from dw::test::Tests
import * from dw::test::Asserts

import * from MyModule
---
"MyModule" describedBy [
   "something" describedBy [
       "It should do something" in do {
           something() must beObject()
       },
   ],
]
dataweave

Create an Integration Test for a Module

To create an integration test for a module:

  1. In your test folder, create a DataWeave mapping that uses the module you want to test.

  2. Follow the steps in Creating an Integration Test for a Mapping.

Run the Tests from the Command Line

You can run the tests from the command line by running the mvn test command:

  • To run a single test, use mvn -Dtest=<MyMappingTest> test.

  • To skip a test, use mvn install -DskipTests.

Matchers

The testing framework includes a set of matchers to use in your tests. See the DataWeave assertions module for reference information.