---
schema_version: "1"
id: 532d29fee0e63dfcb72946be7ba128d69dfad4185d8a6dec48e8dbe3f4220c2f
title: Mocking Resources for Your Tests
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/munit/2.3/mock-file-cookbook.md
canonical_url: https://docs.mulesoft.com/munit/2.3/mock-file-cookbook
source_site: docs.mulesoft.com
content_type: documentation
content_hash: aaf1e076daaee4b6cd54da9b97964a4f1a9dc4cab1f911b2d6017f8e080d4bed
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
---
product: MUnit
version: 2.3
---

# Mocking Resources for Your Tests

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

As your tests suites start to get bigger and more complex, it’s a good practice to externalize some
parts of the test code, not only so that you can keep the code clean and organized but also to reuse
the files that you need to mock for your tests.

You can leverage DataWeave to mock your test code, either by creating DataWeave scripts or by using
variables in a DataWeave module.

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