Munits
Munits
mule-studio mule-esb munit. we are observing a behavior where in the variables set in the
"before-call" and "after-call" blocks of the spy munit processor are not being recognized in
the actual mule flows.
A Mule event contains the core information processed by the runtime. ... The event source
produces a Mule event. The Mule event travels sequentially through the components of a
flow.
Assert that ?
The Assert That event processor allows you to run assertions to validate the state of a Mule
event’s content. You can use the Assert That processor to validate the Mule event after the
production code runs.
The Assert That processor uses a set of DataWeave functions called MUnit matchers to define
the assertion conditions for any value in an expression.
For example, to assert that a payload is equal to a certain value, configure the Assert That
processor using the equalTo() matcher.
<munit-tools:assert-that
expression="#[payload]"
is="#[MunitTools::equalTo('example')]"
message="The payload should be ‘example’"/>
Assert Equals ?
The Assert Equals processor allows you to run assertions in order to validate the state of a
Mule Event’s content. This processor can be used to validate the Mule Event after the
production code runs.
For example, to assert that a payload is equal to a certain value, you can configure the
Assert-Equals processor the following way:
<munit-tools:assert-equals
actual="#[payload]"
expected="#[2]"
message="The payload should be 2"/>
The Mock When processor allows you to mock an event processor when it matches the defined
name and attributes.
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 the with-
attribute element to define the attribute’s name and value. In the example above you are
defining a POST method.
The Verify Event processor allows you to verify if a processor was called.
For example, you can validate if a specific processor has been called with a particular set of
attributes a specific number of times.
When defining a verification, you are telling MUnit to fail a test if the verification is not successful.
You can define verifications over any processor, even if you haven’t created a mock for it.
The example below shows how to define a simple payload of a Mule Message:
<munit:set-event cloneOriginalEvent="false">
<munit:payload value="#['payload_value']" mediaType="text/plain"
encoding="UTF-8"/>
</munit:set-event>
The set-event has the cloneOriginalEvent property. If set to true, it clones the event produced by
your code.
By default, the value of this property is false.
The payload element in the Set Event processor has the following attributes:
mediaType Defines the mime type of the message. This attribute is optional.
The Storage processors allows you to manage temporary storage during your tests.
Store operation
Stores the given data associated with the given key. If value not set, it defaults to
the payload .
<munit-tools:store key="myKey">
<munit-tools:value>#[person : { name: 'John', lastName:
'Smith' }]</munit-tools:value>
</munit-tools:store>
Retrieve operation
<munit-tools:retrieve key="myKey"/>
Remove operation
Removes and returns the data associated with the given key
<munit-tools:remove key="myKey"/>
The Queue event processors allow you to manage temporary queues during your test runs.
Queue
Adds the value to the queue. If not specified, the value is taken from the payload .
<munit-tools:queue>
<munit-tools:value>#[1]</munit-tools:value>
</munit-tools:queue>
You can specify the queue’s name to which the value should be added. If not specified, the
default queue will be used.
<munit-tools:queue queueName="CUSTOM_QUEUE_1">
<munit-tools:value>#[1]</munit-tools:value>
</munit-tools:queue>
Dequeue
Removes the last element of the queue, if the queue is empty, it waits until an element is
queued.
<munit-tools:dequeue/>
Like the queue operation, you can specify the queue’s name from which to dequeue. If not
specified, the default queue will be used.
<munit-tools:dequeue queueName="CUSTOM_QUEUE_1"/>
Queue Size
<munit-tools:assert-that expression="#[MunitTools::queueSize()]"
is="#[MunitTools::equalTo(2)]" />
Like the other operations, you can specify the queue from which to the get the size. If not
specified, the default queue will be used.
<munit-tools:assert-that
expression="#[MunitTools::queueSize('CUSTOM_QUEUE_1')]"
is="#[MunitTools::equ