The MUnit Mock When processor intercepts a specified event processor during a MUnit test and returns a configured response instead of executing the real operation. Use it to isolate the flow under test from external dependencies such as HTTP requests, database calls, or web service operations.
For example, you can use the Mock Event processor to mock a POST request with a mocked payload:
<munit-tools:mock-when processor="http:request">
<munit-tools:with-attributes>
<munit-tools:with-attribute attributeName="method" whereValue="#['POST']"/>
</munit-tools:with-attributes>
<munit-tools:then-return>
<munit-tools:payload value="#['mockPayload']"/>
</munit-tools:then-return>
</munit-tools:mock-when>
You can set the processor attribute to define the processor to mock and set the with-attribute element to define the attribute name and value. In the previous example, you define a POST method.
You can also use DataWeave functions and mappings to set the value attribute in the then-return element. For example, create a mockPost.dwl in the src/test/resources/sample_data file:
%dw 2.0
output application/json
---
{
"foo" : "var"
}
This DataWeave file creates the payload to send in a POST request. You can then use the readUrl DataWeave function to read the mapping file:
<munit-tools:then-return>
<munit-tools:payload value="#[readUrl('classpath://sample_data/mockPost.dwl')]" mediaType="application/json" encoding="UTF-8" />
</munit-tools:then-return>
Finally, you can configure a then-return element to define the type of response the mocked processor returns. It can be a payload, a variable, a list of attributes, or an error.
You can use either a then-return to return a static, constant value, or you can use a then-call to invoke a flow for cases where the returned value can change over time, or to have different responses according to certain inputs.
|
|
The mocking of the processors is executed after the initialization of the Mule app, and some connectors can require credentials or configurations to start the app, even when its processors are mocked.
|
|
|
Logger processor and Transform Message processor (DataWeave) cannot be mocked.
|