如何使用MUnit XML验证出站属性?

时间:2015-12-24 16:17:27

标签: mule mule-component munit

我在将有效负载发送到JMS队列之前在消息中设置了一些出站属性,我想测试在将消息发送到JMS队列之前在消息中正确设置了这些属性。

我考虑过在JMS出站端点之前使用MUnit Spy,但是间谍只能验证会话属性,调用属性和有效负载。有没有其他方法可以使用MUnit XML实现这一目标?

我创建了一个迷你骡子项目来进一步说明问题。代码如下。本质上,它只是一个调用子流的流,它在mule消息中设置出站属性,而MUnit有一个spy,它断言在调用子流之后在mule消息中设置了出站属性。但是,MUnit Spy似乎无法访问mule出站属性。我想知道此时是否还有另一种解决方法。我知道文档指定间谍只能验证会话和调用属性,以及此时的有效负载。任何建议都是受欢迎的。

Sandbox.xml - 主骡文件

    <?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文件 - 测试主流

<?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>

谢谢,

1 个答案:

答案 0 :(得分:1)

不要为此使用'间谍',它有一些限制。看一下文档:

  

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

您可以在流程参考之后添加断言。

<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>