Reading Time: 4 minutes

This tutorial continues from Part 1.

Mock is a feature provided by MUnit to mock the behavior of the message processor. Basically, MUnit replaces the actual behavior of message processor with the behavior defined by the user.

latest report
Learn why we are the Leaders in management and

There are various scenarios where we can use the Mock Message processor. Imagine we have completed the development of a Mule application and need to test it, but in the application, there is a database connector in the message processor region and the database is not ready to accept any request; but, we still want to test the application. In such cases, we can mock the normal database connector behavior.

There can be more scenarios, like we are connecting to JMS queue but JMS is not ready, and we want to test the application. In such cases, we can again make use of mock message processor.

Let's walk through how to use the Mock component in MUnit test. Below is the flow for which we will create MUnit tests and will mock set-payload component.

munit mock messaging

Right click the flow and create the MUnit tests. It will create MUnit tests in the folder src/test/munit of your application.

munit mulesoft

Define the Message Processor to Mock

Drag and drop the Mock component in the setup region of MUnit test.

munit message processor
munit testing
 messageProcessor  is used to specify the message processor to be mocked. In this case, we have mock set-payload component.

Define the Mock with Attributes

When we have more than one similar component (i.e. 2 set-payload) in our Mule flow,  we want to mock only one set payload. In that case, we need to add attributes in the Mock component, which accepts the name and value of message processor.

Below is the Mule flow with 2 set-payload. We need to mock only one component.

munit mock messaging

We can define attributes as shown below to mock one set-payload.

munit mock mulesoft
 
munit testing
  
  •  name  is the name of the attribute and it cannot be MEL expressions.
  •  whereValue  is the value that the attribute of the real message processor should contain. It accepts MEL expressions.

And that is it! For a video walkthrough, please refer to the tutorial below:

This blog post is from a Champion and first appeared on Dzone.