<dependency>
<groupId>org.mule.weave</groupId>
<artifactId>data-weave-testing-framework</artifactId>
<version>${data.weave.testing.framework.version}</version>
<scope>test</scope>
</dependency>
xml
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:
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 |
|
DataWeave Test |
The |
|
Input |
The file that contains the input to evaluate against the mapping. Depending on the test, this file is optional. |
|
Output |
The file that contains the output to check against the results of the mapping. |
|
Create an Integration Test for a Mapping
To test a DataWeave mapping, create an integration test by following these steps:
-
Create a
.dwl
file with a name ending inTest
in thesrc/test/dw/<package>/<testFileName>
folder. -
In the test file’s header, import the
dw::test::Tests
anddw::test::Asserts
modules. -
In the test file’s body, nest any number of sections (
“sectionName”
), followed bydescribedBy
, and specify the test name at the end (“testName”
), followed byin do {<codeExecution/assertion>}
. -
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 functionoutputFrom(<mapping/scenario>)
-
-
Add the expected inputs and outputs to the
src/test/resources/<mappingPath>
folder. -
If you want to configure the reader properties for the inputs, create a properties file with suffix
{fileName}-config.properties
in thesrc/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:
-
Create a
.dwl
file with a name ending inTest
in thesrc/test/dw/<package>/<testFileName>
folder. -
In the test file’s header, import
dw::test::Tests
,dw::test::Asserts
, and the custom module you want to test, for example,"MyModule"
. -
In the test file’s body, nest any number of sections (
“sectionName”
) followed bydescribedBy
, and specify the test name at the end (“testName”
). -
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
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:
-
In your test folder, create a DataWeave mapping that uses the module you want to test.
-
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.