How to set a File as a Testing Payload for Munits

While working with Munits, we use the Set Event processor to set any initial payload and attributes for testing purposes. Sometimes, in our actual flows, instead of the payload in the body of HTTP Request, we expect a file containing the complete payload. These files could be CSV, Json, PDF, text file, or any other kind of file. In such cases, we can use getResourceAsString function from MunitTools . The dataweave expression to call this function is as following

MunitTools::getResourceAsString('example.csv')

We need to pass the path and name of the file that we want to use as an argument to getResourceAsString as stated in the above code snippet. By default, the path will be set to src/test/resources .

Now that we know what the expression is, let's see an example of how to use it exactly.

Drag and drop Set Event component from Munit module into the canvas.

In the properties of Set Event component write above expression for getting a file in the payload section. Also, write this expression in #[] so that it will be evaluated as a dataweave script by anypoint studio.

Set proper Media Type and Encoding depending on the extension of your test payload file. In the above exhibit, it is set to application/csv and encoding as UTF-8 according to the requirements.

You can also notice the argument given to getResourceAsString function is 'batchFIles/teams.csv' which simply means that the file I want to use is in the folder batchFiles inside src/test/resources folder. You can see the same in the exhibit below.

just run your test case now, this is all you need to do to set a file as a testing payload for Munits.

Hope this was helpful!

Give a clap if you found this article useful, suggestions are welcome in the comment.

If you want to connect with me, find me here:

Keep it simple! stay happy!