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

dw::test::Asserts

This module contains the set of core matchers to use in your tests.

Example

This example validates if a payload is of type Object.

Source

%dw 2.0
import dw::tests::Asserts
---
payload must beObject()
DataWeave

Functions

anyOf

anyOf(matchers: Array<Matcher<Any>>): Matcher<Any>

Validates that the value satisfies at least one of the given matchers.

Parameters
Name Description

matchers

The list of matchers to be tested.

Example

This example shows how to assert that a value must be of type Object or String.

Source
%dw 2.0
import dw::tests::Asserts
---
"A Text" must anyOf(beObject(), beString())
DataWeave

beArray

beArray(): Matcher

Validates that a given value is of type Array.

Example

This example shows how to validate that a value is of type Array.

Source
%dw 2.0
import dw::tests::Asserts
---
[1, 4, 7] must beArray()
DataWeave

beBlank

beBlank(): Matcher<String | Null>

Validates that the String value is blank.

Example

Validates that the string is empty or has whitespaces.

Source
%dw 2.0
import dw::tests::Asserts
---
"  " must beBlank()
DataWeave

beBoolean

beBoolean(): Matcher

Validates that a given value is of type Boolean.

Example

This example shows how to validate that a value is of type Boolean.

Source
%dw 2.0
import dw::tests::Asserts
---
true must beBoolean()
DataWeave

beEmpty

beEmpty(): Matcher<String | Object | Array | Null>

Validates that the value (of the type String, Object, or Array) is empty.

Example

Validates that the array is of size 0.

Source
%dw 2.0
import dw::tests::Asserts
---
[] must beEmpty()
DataWeave

beGreaterThan

beGreaterThan(expected: Comparable, inclusive: Boolean = false): Matcher<Comparable>

Validates that the asserted Comparable value is greater than the given one.

The value can be equal when using the inclusive argument.

Parameters
Name Description

expected

The number to compare to.

inclusive

Specifies if the comparison is inclusive or not (Optional). False by default.

Example

Expects a Number to be greater than 2.

Source
%dw 2.0
import dw::tests::Asserts
---
3 must beGreaterThan(2)
DataWeave
Example

Expects a number to be greater or equal than 2.

Source
%dw 2.0
import dw::tests::Asserts
---
3 must beGreaterThan(2, true)
DataWeave

beLowerThan

beLowerThan(expected: Comparable, inclusive: Boolean = false): Matcher<Comparable>

Validates that the asserted Comparable value is less than the given value.

The value can be equal when using the inclusive argument.

Parameters
Name Description

expected

The number to compare to.

inclusive

Specifies if the comparison is inclusive or not (Optional). False by default.

Example

Expects a Number to be less than 2.

Source
%dw 2.0
import dw::tests::Asserts
---
1 must beLowerThan(2)
DataWeave
Example

Expects a Number to be less or equal than 2.

Source
%dw 2.0
import dw::tests::Asserts
---
1 must beLowerThan(2, true)
DataWeave

beNull

beNull(): Matcher

Validates that a given value is of type Null.

Example

This example shows how to validate that a value is of type Null.

Source
%dw 2.0
import dw::tests::Asserts
---
null must beNull()
DataWeave

beNumber

beNumber(): Matcher

Validates that a given value is of type Number.

Example

This example shows how to validate that a value is of type Number.

Source
%dw 2.0
import dw::tests::Asserts
---
123 must beNumber()
DataWeave

beObject

beObject(): Matcher

Validates that a given value is of type Object.

Example

This example shows how to validate that a value is of type Object.

Source
%dw 2.0
import dw::tests::Asserts
---
{ name : "Lionel", lastName: "Messi"} must beObject()
DataWeave

beOneOf

beOneOf(expected: Array<Any>): Matcher

Validates that the value is contained in the given array.

Parameters
Name Description

expected

The array of expected elements.

Example

Asserts that the value is either 1 or "A Text" or true.

Source
%dw 2.0
import dw::tests::Asserts
---
1 must beOneOf([1, "A Text", true])
DataWeave

beString

beString(): Matcher

Validates that a given value is of type String.

Example

This example shows how to validate that a value is of type String.

Source
%dw 2.0
import dw::tests::Asserts
---
"A Text" must beString()
DataWeave

contain

contain(expected: String): Matcher<String>

Validates that the asserted string contains the given string.

Parameters
Name Description

expected

The text expected to be contained within the input string.

Example

Expects the value to contain the String value "ex".

Source
%dw 2.0
import dw::tests::Asserts
---
"A Text" must contain("ex")
DataWeave

contain(expected: Any): Matcher<Array<Any>>

Validates that the asserted array contains the given value.

Parameters
Name Description

expected

The value expected to be in the array.

Example

Expects the Array value to contain the Number value 1.

Source
%dw 2.0
import dw::tests::Asserts
---
[1, "A Text", true] must contain(1)
DataWeave

eachItem

eachItem(matcher: Matcher<Any>): Matcher<Array<Any>>

Validates that each item of the array satisfies the given matcher.

Parameters
Name Description

matcher

The matcher to apply to all the elements.

Example

Expects all the elements in the array to be a number.

Source
%dw 2.0
import dw::tests::Asserts
---
[1,2,3] must eachItem(beNumber())
DataWeave

endWith

endWith(expected: String): Matcher<String>

Validates that the asserted String value ends with the given string.

Parameters
Name Description

expected

Suffix of the 'String' value.

Example

Expects the String value to end with "xt".

Source
%dw 2.0
import dw::tests::Asserts
---
"A Text" must endWith("xt")
DataWeave

equalTo

equalTo(expected: Any, equalToConfig: { unordered?: Boolean } = {}): Matcher<Any>

Validates that a value is equal to another one.

Parameters
Name Description

expected

The expected value.

equalToConfig

Configuration of how to compare them.

Example

This example shows how to assert that a value must be equal to 3.

Source
%dw 2.0
import dw::tests::Asserts
---
(1 + 2) must equalTo(3)
DataWeave

equalToResource

equalToResource(resourceName: String, contentType: String = "application/dw", readerProperties: Object = {}): Matcher<Any>

Validates that the given value is equal to the content of a resource file.

The resource file must belong to the classpath.

Parameters
Name Description

resourceName

The resource name.

contentType

The content type of the resource (Optional).

readerProperties

An object with the configuration properties (Optional).

Example

Expects a value to be equal to the content of the resource user.json.

Source
%dw 2.0
import dw::tests::Asserts
---
{ name: "Lionel", lastName: "Messi" } must equalToResource("user.json", "application/json")
DataWeave

haveItem

haveItem(matcher: Matcher<Any>): Matcher<Array<Any>>

Validates that at least one item of the array satisfies the given matcher.

Parameters
Name Description

matcher

The matcher to apply to at least one of the elements.

Example

Expects that one element of the array is a number.

Source
%dw 2.0
import dw::tests::Asserts
---
[1, true, "a text"] must haveItem(beNumber())
DataWeave

haveKey

haveKey(keyName: String): Matcher<Object>

Validates that the object has the given key.

Parameters
Name Description

keyName

The name of the key to expect to be present.

Example

Validates that the Object contains a key called "name".

Source
%dw 2.0
import dw::tests::Asserts
---
{ name: "Lionel", lastName: "Messi" } must haveKey("name")
DataWeave

haveSize

haveSize(expectedSize: Number): Matcher<Array | String | Object | Null>

Validates that the array has the given size.

Parameters
Name Description

expectedSize

The expected array size

Example

Expects that the array must be of size 3.

Source
%dw 2.0
import dw::tests::Asserts
---
[1, 4, 7] must haveSize(3)
DataWeave

haveValue

haveValue(value: Any): Matcher<Object>

Validates that the Object has the given value.

Parameters
Name Description

value

The value that is expected to be present

Example

Expected that the Object contains the value "Messi".

Source
%dw 2.0
import dw::tests::Asserts
---
{ name: "Lionel", lastName: "Messi" } must haveValue("Messi")
DataWeave

must

must<T>(value: T, matchExpressions: Array<(value: T) → Matcher<T> | MatcherResult | Boolean>): MatcherResult

This function enables you to assert a value with with a list of matchers or expressions.

Parameters
Name Description

value

matchExpressions

Example

This example shows how to assert that a payload is of type Object and has a property foo that is null.

Source
%dw 2.0
import dw::tests::Asserts
---
payload must [
    beObject(),
    $.foo is Null
]
DataWeave

must<T>(value: T, matcher: (value: T) → Matcher<T> | Boolean): MatcherResult

This function enables you to assert a value with a matcher of expressions.

Parameters
Name Description

value

The value to assert.

matcher

The matcher to use.

Example

This example shows how to assert that a payload is of type Object.

Source
%dw 2.0
import dw::tests::Asserts
---
payload must beObject()
DataWeave

notBe

notBe<T>(matcher: Matcher<T>): Matcher<T>

Validates that the value doesn’t satisfy the given matcher.

Parameters
Name Description

matcher

The matcher to negate.

Example

This example shows how to assert that a value mustn’t be equal to 2.

Source
%dw 2.0
import dw::tests::Asserts
---
1 must notBe(equalTo(2))
DataWeave

notBeNull

notBeNull(): Matcher

Validates that a given value isn’t of type Null.

Example

This example shows how to validate that a value isn’t of type Null.

Source
%dw 2.0
import dw::tests::Asserts
---
"A Text" must notBeNull()
DataWeave

startWith

startWith(expected: String): Matcher<String>

Validates that the asserted string starts with the given String value.

Parameters
Name Description

expected

Prefix of the 'String' value.

Example

Validates that the string starts with "A".

Source
%dw 2.0
import dw::tests::Asserts
---
"A Text" must startWith("A")
DataWeave

Variables

MATCHED

Constant that represents a successful match.

Types

Matcher

Data type that represents a matcher to perform assertions.

Example
Source
%dw 2.0
import dw::tests::Asserts

fun beEqualToOne(): Matcher<Any> =
    (actual) -> do {
        {
            matches: actual == 1,
            description: { expected: "To be 1", actual: write(actual) as String }
        }
    }
DataWeave
Definition
(value: T) -> MatcherResult
DataWeave

MatcherResult

Data Type that represents the result of an assertion.

Example
Source
{
  "matches": false,
  description : { expected : "Number type", actual: "A Text" }
}
DataWeave
Definition
{ matches: Boolean, description: { expected: String, actual: String }, reasons?: Array<String> }
DataWeave