dw::test::Asserts | MuleSoft Documentation
# 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()
```

## Functions
### anyOf
#### anyOf(matchers: Array<Matcher<Any>>): Matcher<Any>
Validates that the value satisfies at least one of the given matchers.
##### Parameters
Name | Description  
---|---  
The list of matchers to be tested.  
##### Example
This example shows how to assert that a value must be of type `Object` or `String`.
```
%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`.
```
%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.
```
%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`.
```
%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`.
```
%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
Name | Description  
---|---  
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`.
```
%dw 2.0
import dw::tests::Asserts
---
3 must beGreaterThan(2)
```

##### Example
Expects a number to be greater or equal than `2`.
```
%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
Name | Description  
---|---  
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`.
```
%dw 2.0
import dw::tests::Asserts
---
1 must beLowerThan(2)
```

##### Example
Expects a `Number` to be less or equal than `2`.
```
%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`.
```
%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`.
```
%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`.
```
%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
Name | Description  
---|---  
The array of expected elements.  
##### Example
Asserts that the value is either `1` or "A Text" or `true`.
```
%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`.
```
%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
Name | Description  
---|---  
The text expected to be contained within the input string.  
##### Example
Expects the value to contain the `String` value "ex".
```
%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
Name | Description  
---|---  
The value expected to be in the array.  
##### Example
Expects the `Array` value to contain the `Number` value 1.
```
%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
Name | Description  
---|---  
The matcher to apply to all the elements.  
##### Example
Expects all the elements in the array to be a number.
```
%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
Name | Description  
---|---  
Suffix of the 'String' value.  
##### Example
Expects the `String` value to end with "xt".
```
%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
Name | Description  
---|---  
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`.
```
%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
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`.
```
%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
Name | Description  
---|---  
The matcher to apply to at least one of the elements.  
##### Example
Expects that one element of the array is a number.
```
%dw 2.0
import dw::tests::Asserts
---
[1, true, "a text"] must haveItem(beNumber())
```
### 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`.
```
%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
Name | Description  
---|---  
The matcher to apply to at least one of the elements.  
##### Example
Expects that one element of the array is a number.
```
%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
Name | Description  
---|---  
The name of the key to expect to be present.  
##### Example
Validates that the `Object` contains a key called "name".
```
%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
Name | Description  
---|---  
`expectedSize` | The expected array size  
##### Example
Expects that the array must be of size `3`.
```
%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
Name | Description  
---|---  
The value that is expected to be present  
##### Example
Expected that the `Object` contains the value "Messi".
```
%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
Name | Description  
---|---  
`matchExpressions`  
##### Example
This example shows how to assert that a `payload` is of type `Object` and has a property `foo` that is `null`.
```
%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
Name | Description  
---|---  
The value to assert.  
The matcher to use.  
##### Example
This example shows how to assert that a `payload` is of type `Object`.
```
%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
Name | Description  
---|---  
The matcher to negate.  
##### Example
This example shows how to assert that a value mustn’t be equal to 2.
```
%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`.
```
%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
Name | Description  
---|---  
Prefix of the 'String' value.  
##### Example
Validates that the string starts with "A".
```
%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
```
%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
```
(value: T) -> MatcherResult
```

### MatcherResult
Data Type that represents the result of an assertion.
#### Example
##### Source
```
{
 "matches": false,
 description : { expected : "Number type", actual: "A Text" }
}
```

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