1. dw::test::Tests
This module contains all functions required to create a DataWeave test
1.1. Example
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
%dw 2.0
import * from dw::test::Tests
---
"Matcher api" describedBy [
"It should support nested matching" in do {
var payload = {}
---
payload must [
beObject(),
$.foo must [
beNull()
]
]
},
"It should support simple matching" in do {
var payload = {}
---
payload must beObject()
},
"It should support multiple root cases" in do {
var payload = {}
var flowVar = {a: 123}
---
[
payload must beObject(),
flowVar must [
beObject(),
$.a must equalTo(123)
]
]
},
"It should support using custom assertions" in do {
var payload = []
---
payload must sizeOf($) > 2
}
]
1.2. Functions
1.2.1. describedBy
describedBy(suite: String, testsToRun: Array<() -> TestResult>): TestResult
Defines a new test suite with the list of test cases.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
%dw 2.0
import * from dw::test::Tests
---
"Matcher api" describedBy [
"It should support nested matching" in do {
var payload = {}
---
payload must [
beObject(),
$.foo must [
beNull()
]
]
},
]
1.2.2. evalPath
evalPath(dwlFilePath: String, context: Object, mimeType: String): Any
Runs a specific mapping with the given context and mimetype.
Parameters
Name | Description |
---|---|
dwlFilePath |
Path inside the classpath where the runnable dataweave file is present. |
context |
Object which contains different input values (each input name would be the key of the object). |
mimeType |
The default mimetype if not specified on the file |
Example
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import * from dw::test::Tests
import * from dw::test::Asserts
---
"Test MyMapping" describedBy [
"Assert SimpleScenario" in do {
evalPath("MyMapping.dwl", inputsFrom("MyMapping/SimpleScenario"), "application/json" ) must
equalTo(outputFrom("MyMapping/SimpleScenario"))
}
]
evalPath(testUrl: { content: String, url: String }, context: Object, mimeType: String): Any
Evals a test with a given input values as a context and using the specified mimeType as default one when not specified in the file
Parameters
Name | Type | Description |
---|---|---|
|
{ content: String, url: String } |
The test |
|
Object |
The inputs |
|
String |
The default mimetype |
1.2.3. in
in<Ctx <: Object>(testSetup: { config: TestConfig<Ctx>, testName: String }, test: (c: Ctx) -> MatcherResult): TestResult
Defines a new test case inside a test suite with its relevant context. Intended to be used in combination with withConfig
Example
1
2
3
4
5
6
7
8
9
var config = {
setup: () -> { contextString: "context" },
teardown: () -> {}
}
---
"It should generate context for following tests" withConfig config in do {
$.contextString must beString()
}
in<Ctx <: Object>(testSetup: { config: TestConfig<Ctx>, testName: String }, test: Array<(c: Ctx) -> MatcherResult>): TestResult
Defines multiple new test cases inside a test suite that share the same context. Intended to be used in combination with withConfig
Example
1
2
3
4
5
6
7
8
9
10
var config = {
setup: () -> { contextString: "context" },
teardown: () -> {}
}
---
"It should generate context for following tests" withConfig config in [
do { $.contextString must beString() },
do { $.otherContext must equalTo(3) }
]
in<T>(testName: String, testCases: (Null) -> MatcherResult): TestResult
Defines a new test case inside a test suite with a single assertion.
Example
1
2
3
"It should support nested matching" in do {
"foo" must beString()
}
in(testName: String, callback: Array<(Null) -> MatcherResult>): TestResult
Defines a new test case with multiple assertions
Example
1
2
3
4
5
6
7
8
9
10
11
12
"It should support multiple root cases" in do {
var payload = {}
var flowVar = {a: 123}
---
[
payload must beObject(),
flowVar must [
beObject(),
$.a must equalTo(123)
]
]
}
1.2.4. inputsFrom
inputsFrom(dir: String): { _?: Any }
Builds an object with all the inputs to be used as context for a specific mapping.
Parameters
Name | Description |
---|---|
dir |
Directory where to look for the inputs folder and build the context from. |
1.2.5. outputFrom
outputFrom(dir: String)
Returns the result of reading the expected output
Parameters
Name | Description |
---|---|
dir |
Directory where to look for the out file to make assertions on your mapping test. |
1.2.6. withConfig
withConfig<Ctx <: Object>(testName: String, config: TestConfig<Ctx>)
Generates configuration for a test(s) that needs setup/teardown stages. Intended for it to used in combination with
the in
function.
Example
1
2
3
4
5
6
7
8
9
10
var config = {
setup: () -> { contextString: "context", otherContext: 3 },
teardown: () -> {}
}
---
"It should generate context for following tests" withConfig config in [
do { $.contextString must beString() },
do { $.otherContext must equalTo(3) }
]
1.3. Variables
1.3.1. ERROR_STATUS
1.3.2. FAIL_STATUS
1.3.3. OK_STATUS
1.3.4. SKIP_STATUS
1.4. Types
1.4.1. TEST_STATUS
Data Type that describes the result of a Test Execution
1
"ERROR" | "OK" | "FAIL" | "SKIP"
1.4.2. TestConfig
1
{| setup: () -> Ctx, teardown: (c: Ctx) -> Any |}
1.4.3. TestResult
1
{ name: String, time: Number, status: TEST_STATUS, tests?: Array<TestResult>, errorMessage?: String, skipReason?: String, location?: Location }