0
votes

I am setting some outbound properties in the message prior to sending the payload to a JMS queue, and I wanted to test that those properties are properly set in the message prior to sending it out to the JMS queue.

I thought about using an MUnit Spy before the JMS Outbound Endpoint, but the spy is only capable of validating the session properties, invocation properties, and the payload. Is there another way that I can achieve this using MUnit XML?

I created a mini mule project to illustrate the issue further. The code is provided below. Essentially it is just a flow that calls a sub-flow that sets outbound properties in a mule message, and the MUnit has a spy that asserts that the outbound properties are set in the mule message after the sub-flow is called. However, the MUnit Spy doesn't seem to have access to the mule outbound properties. I would like to know if there is another way around this at this time. I know that the docs specify that the spy is only capable of validating session and invocation properties, and the payload at this time. Any suggestion is welcome.

Sandbox.xml - Main Mule File

    <?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <flow name="outbound-props-flow">
        <flow-ref name="outbound-props-sub-flow" doc:name="Call outbound-props-sub-flow"/>
        <logger message="#[message]" level="INFO" doc:name="Log Message"/>
    </flow>
    <sub-flow name="outbound-props-sub-flow">
        <set-property propertyName="outbound-prop-1" value="test1" doc:name="set-outbound-prop-1"/>
        <set-property propertyName="outbount-prop-2" value="test2" doc:name="set-outbound-prop-2"/>
    </sub-flow>
</mule>

MUnit File - Testing the Main Flow

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:mock="http://www.mulesoft.org/schema/mule/mock" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/mock http://www.mulesoft.org/schema/mule/mock/current/mule-mock.xsd">
    <munit:config name="munit" doc:name="MUnit configuration"/>
    <spring:beans>
        <spring:import resource="classpath:sandbox.xml"/>
    </spring:beans>
    <munit:test name="outbound-props-flow-outbound-props-flowTest" description="Test">
        <mock:spy messageProcessor="mule:sub-flow" doc:name="Spy">
            <mock:with-attributes>
                <mock:with-attribute name="name" whereValue="#[matchContains('outbound-props-sub-flow')]"/>
            </mock:with-attributes>
            <mock:assertions-after-call>
                <logger message="Message in SPY Module: #[message]" level="INFO" doc:name="Log Message in Spy"/>
                <munit:assert-on-equals message="outbound-props-1 is not set properly" expectedValue="test1" actualValue="#[message.outboundProperties['outbound-prop-1']]" doc:name="Assert outbound-props-1 is set properly"/>
                <munit:assert-on-equals message="outbound-props-2 is not set properly" expectedValue="test1" actualValue="#[message.outboundProperties['outbound-prop-2']]" doc:name="Assert outbound-props-2 is set properly"/>
            </mock:assertions-after-call>
        </mock:spy>
        <flow-ref name="outbound-props-flow" doc:name="Flow-ref to outbound-props-flow"/>
    </munit:test>
</mule>

Thanks,

Juan

1

1 Answers

2
votes

Don't use 'Spy' for this, it has some limitations. Take a look to the documentation:

https://docs.mulesoft.com/mule-user-guide/v/3.7/the-spy-message-processor#defining-spy-actions

You could simply add the asserts after the flow ref.

<munit:test name="outbound-props-flow-outbound-props-flowTest" description="Test">  
    <flow-ref name="outbound-props-flow" doc:name="Flow-ref to outbound-props-flow"/>
    <munit:assert-on-equals message="outbound-props-1 is not set properly" expectedValue="test1" actualValue="#[message.outboundProperties['outbound-prop-1']]" doc:name="Assert outbound-props-1 is set properly"/>
    <munit:assert-on-equals message="outbound-props-2 is not set properly" expectedValue="test2" actualValue="#[message.outboundProperties['outbound-prop-2']]" doc:name="Assert outbound-props-2 is set properly"/>
</munit:test>