Getting started Community Training Tutorials Documentation APIs, AI & Tools
<flow name="testFlow">
<http:request path="/my/api" />
</flow>
The MUnit Spy event processor observes a specified event processor during a MUnit test, running assertions or verifications before and after the processor executes without modifying the original Mule event. Use it to validate the state of the event at specific points in a flow.
Assume that you have this configuration:
<flow name="testFlow">
<http:request path="/my/api" />
</flow>
You can configure the spy processor to spy on any Http Request and assert that the payload is null before reaching the component, and not null after it’s been processed:
<munit:test name="suiteTest" >
<munit:behavior >
<munit-tools:spy processor="http:request">
<munit-tools:before-call >
<munit-tools:assert-that expression="#[payload]" is="#[MunitTools::nullValue()]"/>
</munit-tools:before-call>
<munit-tools:after-call >
<munit-tools:assert-that expression="#[payload]" is="#[MunitTools::notNullValue()]"/>
</munit-tools:after-call>
</munit-tools:spy>
</munit:behavior>
</munit:test>
Or even to spy on any Http Request, whose path is '/my/api'.
<munit:test name="suiteTest" >
<munit:enable-flow-sources >
<munit:enable-flow-source value="testFlow" />
</munit:enable-flow-sources>
<munit:behavior >
<munit-tools:spy processor="http:request">
<munit-tools:with-attributes >
<munit-tools:with-attribute attributeName="path" whereValue="#['/my/api']" />
</munit-tools:with-attributes>
<munit-tools:before-call >
<munit-tools:assert-that expression="#[payload]" is="#[MunitTools::nullValue()]"/>
</munit-tools:before-call>
<munit-tools:after-call >
<munit-tools:assert-that expression="#[payload]" is="#[MunitTools::notNullValue()]"/>
</munit-tools:after-call>
</munit-tools:spy>
</munit:behavior>
</munit:test>
As shown above, this processor enables you to define actions for before and after calling the spied processor.
However, keep in mind that the spy processor does not modify the original Mule Event.
If you set variables, attributes, or modify the payload inside a Spy, these changes won’t persist outside the processor.
Overall, you should use the Spy processor to verify or assert.