【问题标题】:How do you validate outbound properties using MUnit XML?如何使用 MUnit XML 验证出站属性?
【发布时间】:2015-12-25 04:51:02
【问题描述】:

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

我曾考虑在 JMS 出站端点之前使用 MUnit Spy,但该 Spy 只能验证会话属性、调用属性和有效负载。是否有另一种方法可以使用 MUnit XML 实现这一目标?

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

Sandbox.xml - 主要 Mule 文件

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

谢谢,

胡安

【问题讨论】:

    标签: mule mule-component munit


    【解决方案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>
    

    【讨论】:

    • 嗨,Davo - 我认为鉴于对间谍的限制,这是正确的方法!谢谢!我认为这应该是 mulesoft 在下一个 munit 版本中修复的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多