---
schema_version: "1"
id: c08621f319f0cfdbed2ed16ca151e839b69e0bb73e0ec96d527356a6cad3bb25
title: dw::test::Asserts
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.11/dw-test-asserts.md
canonical_url: https://docs.mulesoft.com/dataweave/2.11/dw-test-asserts
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 7db6de3e8e9dbdbdcd8ce2eedc3f369135d80e120155b410a27793341883e370
description: MuleSoft Documentation Site
release: "2.11"
latest: false
---
---
product: DataWeave
version: 2.11
---

# dw::test::Asserts

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

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

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

## Functions

### anyOf

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

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

##### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>matchers</code></p></td><td><p>The list of matchers to be tested.</p></td></tr></tbody></table>

##### Example

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

###### Source

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

### 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

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

### beBlank

#### beBlank(): Matcher<String | Null>

Validates that the `String` value is blank.

##### Example

Validates that the string is empty or has whitespaces.

###### Source

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

### 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

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

### 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

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

### 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

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>expected</code></p></td><td><p>The number to compare to.</p></td></tr><tr><td><p><code>inclusive</code></p></td><td><p>Specifies if the comparison is inclusive or not (Optional). False by default.</p></td></tr></tbody></table>

##### Example

Expects a `Number` to be greater than `2`.

###### Source

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

##### Example

Expects a number to be greater or equal than `2`.

###### Source

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

### 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

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>expected</code></p></td><td><p>The number to compare to.</p></td></tr><tr><td><p><code>inclusive</code></p></td><td><p>Specifies if the comparison is inclusive or not (Optional). False by default.</p></td></tr></tbody></table>

##### Example

Expects a `Number` to be less than `2`.

###### Source

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

##### Example

Expects a `Number` to be less or equal than `2`.

###### Source

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

### 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

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

### 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

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

### 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

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

### beOneOf

#### beOneOf(expected: Array<Any>): Matcher

Validates that the value is contained in the given array.

##### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>expected</code></p></td><td><p>The array of expected elements.</p></td></tr></tbody></table>

##### Example

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

###### Source

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

### 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

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

### contain

#### contain(expected: String): Matcher<String>

Validates that the asserted string contains the given string.

##### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>expected</code></p></td><td><p>The text expected to be contained within the input string.</p></td></tr></tbody></table>

##### Example

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

###### Source

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

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

Validates that the asserted array contains the given value.

##### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>expected</code></p></td><td><p>The value expected to be in the array.</p></td></tr></tbody></table>

##### Example

Expects the `Array` value to contain the `Number` value 1.

###### Source

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

### eachItem

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

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

##### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>matcher</code></p></td><td><p>The matcher to apply to all the elements.</p></td></tr></tbody></table>

##### Example

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

###### Source

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

### endWith

#### endWith(expected: String): Matcher<String>

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

##### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>expected</code></p></td><td><p>Suffix of the 'String' value.</p></td></tr></tbody></table>

##### Example

Expects the `String` value to end with "xt".

###### Source

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

### equalTo

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

Validates that a value is equal to another one.

##### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>expected</code></p></td><td><p>The expected value.</p></td></tr><tr><td><p><code>equalToConfig</code></p></td><td><p>Configuration of how to compare them.</p></td></tr></tbody></table>

##### Example

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

###### Source

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

### 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

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>resourceName</code></p></td><td><p>The resource name.</p></td></tr><tr><td><p><code>contentType</code></p></td><td><p>The content type of the resource (Optional).</p></td></tr><tr><td><p><code>readerProperties</code></p></td><td><p>An object with the configuration properties (Optional).</p></td></tr></tbody></table>

##### Example

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

###### Source

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

### haveItem

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

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

##### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>matcher</code></p></td><td><p>The matcher to apply to at least one of the elements.</p></td></tr></tbody></table>

##### Example

Expects that one element of the array is a number.

###### Source

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

### haveKey

#### haveKey(keyName: String): Matcher<Object>

Validates that the object has the given key.

##### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>keyName</code></p></td><td><p>The name of the key to expect to be present.</p></td></tr></tbody></table>

##### Example

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

###### Source

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

### haveSize

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

Validates that the array has the given size.

##### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>expectedSize</code></p></td><td><p>The expected array size</p></td></tr></tbody></table>

##### Example

Expects that the array must be of size `3`.

###### Source

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

### haveValue

#### haveValue(value: Any): Matcher<Object>

Validates that the Object has the given value.

##### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>value</code></p></td><td><p>The value that is expected to be present</p></td></tr></tbody></table>

##### Example

Expected that the `Object` contains the value "Messi".

###### Source

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

### 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

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>value</code></p></td><td></td></tr><tr><td><p><code>matchExpressions</code></p></td><td></td></tr></tbody></table>

##### Example

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

###### Source

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

#### 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

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>value</code></p></td><td><p>The value to assert.</p></td></tr><tr><td><p><code>matcher</code></p></td><td><p>The matcher to use.</p></td></tr></tbody></table>

##### Example

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

###### Source

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

### notBe

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

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

##### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>matcher</code></p></td><td><p>The matcher to negate.</p></td></tr></tbody></table>

##### Example

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

###### Source

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

### 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

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

### startWith

#### startWith(expected: String): Matcher<String>

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

##### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>expected</code></p></td><td><p>Prefix of the 'String' value.</p></td></tr></tbody></table>

##### Example

Validates that the string starts with "A".

###### Source

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

## Variables

### MATCHED

Constant that represents a successful match.

## Types

### Matcher

Data type that represents a matcher to perform assertions.

#### Example

##### Source

```DataWeave
%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 }
        }
    }
```

Definition

```DataWeave
(value: T) -> MatcherResult
```

### MatcherResult

Data Type that represents the result of an assertion.

#### Example

##### Source

```DataWeave
{
  "matches": false,
  description : { expected : "Number type", actual: "A Text" }
}
```

Definition

```DataWeave
{ matches: Boolean, description: { expected: String, actual: String }, reasons?: Array<String> }
```
