---
schema_version: "1"
id: 700248cbeb08f42177a99170c713eb1e835eab19a3e806f79f00f307df44cfa7
title: Mock Resources for MUnit Tests with DataWeave
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/munit/latest/mock-file-cookbook.md
canonical_url: https://docs.mulesoft.com/munit/latest/mock-file-cookbook
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 3f7894bbde3b9f698bd6bf77dfbda97fa31c9de47f41b7617d644660f23dc5cf
description: MuleSoft Documentation Site
release: "3.7"
latest: true
---
---
product: MUnit
version: 3.7
is-latest-version: true
---

# Mock Resources for MUnit Tests with DataWeave

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

![logo acb active](/mulesoft/current/en-us/_/img/icons/anypoint-code-builder.svg "Anypoint Code Builder")Anypoint Code
Builder

![logo studio active](/mulesoft/current/en-us/_/img/icons/studio.svg "Anypoint Studio")Anypoint Studio

Externalize mock data for MUnit tests using DataWeave scripts or modules to keep your test code
clean and reusable. Use DataWeave files or variables in a DataWeave module to centralize mocked
payloads and responses.

## Mock Files Using DataWeave File

You can write a DataWeave file that returns the data that you want to mock, save the file under a
folder in `src/test/resources` and then use a DataWeave function from your test to read this file.

For example, imagine that you have defined a `mockPost.dwl` file in the
`src/test/resources/sample_data` folder with the following content:

```none
%dw 2.0
output application/json
---
{
	"foo" : "var"
}
```

This DataWeave file (`mockPost.dwl`) is a sample that creates a mock payload that you want to return
in an `http:request` request.

When configuring the `then-return` section in the `mock-when`, use the `readUrl` function to read
the mapping file:

```xml
<munit-tools:then-return>
  <munit-tools:payload value="#[readUrl('classpath://sample_data/mockPost.dwl')]" mediaType="application/json" encoding="UTF-8" />
</munit-tools:then-return>
```

The `classpath-based` URL uses the `classpath:` protocol prefix to find the file defined in
`src/test/resources`.

## Mock Files Using Variables in a DataWeave Module

You can create a DataWeave module to centralize all your mocked data in a single file by declaring
it using DataWeave variables.

For example, imagine that you create a custom DataWeave module by creating a `TestData.dwl` file in
`src/test/resources`, and you add variables with references to different mocked data, either from
files or inline:

```none
import getResourceAsString from MunitTools

var mockPost = readUrl('classpath://sample_data/mockPost.dwl')
var foo = "N123"
var jsonFromFile = read(getResourceAsString('sample_data/mydata.json'), 'application/json')
var jsonObject = { paymentID: "1B56925769601335TLQMIWVY" }
```

You can then access any of the defined variables in the DataWeave module by importing the variables
from your module:

```xml
<munit-tools:then-return >
    <munit-tools:variables>
        <munit-tools:variable key="ticket" value="#[import TestData output application/json --- TestData::mockPost]" mediaType="application/json" />
    </munit-tools:variables>
</munit-tools:then-return>
```

## See Also

- [Create Custom Modules and Mappings](https://docs.mulesoft.com/dataweave/latest/dataweave-create-module)
