【问题标题】:Mock a message from a 3rd party system in Mule using MUnit使用 MUnit 在 Mule 中模拟来自第 3 方系统的消息
【发布时间】:2015-06-22 17:21:56
【问题描述】:

我正在为一个处理来自 Magento 实例的新数据的 Mule 应用程序编写一个测试套件(使用 Munit)。我的流程之一是为新客户轮询 Magento,它收到的消息类型为:com.magento.api.CustomerCustomerEntity

我想知道如何模拟它,以便在我的测试用例中,当调用 Magento 消息处理器时,我可以返回相同类型的有效负载并做出适当的断言?

目前我的 Munit 测试如下:

<mock:config name="mock_MagentoToSalesforce" doc:name="Mock configuration"/>
<spring:beans>
    <spring:import resource="classpath:MagentoToSalesforce.xml"/>
    <spring:bean id="myBean" name="myBean" class="com.magento.api.CustomerCustomerEntity">
        <spring:property name="email" value="test@test.com"/>
    </spring:bean>
</spring:beans>

<munit:test name="MagentoToSalesforce-test-getCustomersFlowTest" description="Test">
    <mock:when config-ref="mock_MagentoToSalesforce" messageProcessor=".*:.*" doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute whereValue-ref="#[string:Get New Customers]" name="doc:name"/>
        </mock:with-attributes>
        <mock:then-return payload-ref="#[app.registry.myBean]"/>
    </mock:when>
    <flow-ref name="getCustomers" doc:name="Flow-ref to getCustomers"/>
</munit:test>

我要测试的流程是:

<flow name="getCustomers" processingStrategy="synchronous">
    <poll doc:name="Poll">
        <fixed-frequency-scheduler frequency="30" timeUnit="SECONDS"/>
        <watermark variable="watermark" default-expression="#[new org.mule.el.datetime.DateTime().plusYears(-30)]" update-expression="#[new org.mule.el.datetime.DateTime().plusYears(-0)]" selector-expression="#[new org.mule.el.datetime.DateTime(payload.created_at, 'yyyy-MM-dd HH:mm:ss')]"/>
        <magento:list-customers config-ref="Magento" filter="dsql:SELECT confirmation,created_at,created_in,customer_id,dob,email,firstname,group_id,increment_id,lastname,middlename,password_hash,prefix,store_id,suffix,taxvat,updated_at,website_id FROM CustomerCustomerEntity WHERE updated_at &gt; '#[flowVars.watermark]'" doc:name="Get New Customers"/>
    </poll>
    <foreach doc:name="For Each">
        <data-mapper:transform config-ref="MagentoCustomer_To_SalesforceContact" doc:name="Map Customer to SFDC Contact">
            <data-mapper:input-arguments>
                <data-mapper:input-argument key="ContactSource">Magento</data-mapper:input-argument>
            </data-mapper:input-arguments>
        </data-mapper:transform>
        <flow-ref name="upsertSalesforceContactFlow" doc:name="upsertSalesforceContactFlow"/>
    </foreach>
</flow>

根据 Ryan 的回答更新:

更改了表达式以返回 #[ent = new com.magento.api.CustomerCustomerEntity(); ent.setEmail('test@test.com'); return [ent];] 的有效负载 - 注意,将方法更改为 setEmail 以匹配文档 here。我得到的错误是:

ERROR 2015-06-22 09:58:34,719 [main] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Object "org.mule.transport.NullPayload" not of correct type. It must be of type "{interface java.lang.Iterable,interface java.util.Iterator,interface org.mule.routing.MessageSequence,interface java.util.Collection}" (java.lang.IllegalArgumentException). Message payload is of type: NullPayload
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. Object "org.mule.transport.NullPayload" not of correct type. It must be of type "{interface java.lang.Iterable,interface java.util.Iterator,interface org.mule.routing.MessageSequence,interface java.util.Collection}" (java.lang.IllegalArgumentException)
  org.mule.util.collection.EventToMessageSequenceSplittingStrategy:64 (null)
2. Object "org.mule.transport.NullPayload" not of correct type. It must be of type "{interface java.lang.Iterable,interface java.util.Iterator,interface org.mule.routing.MessageSequence,interface java.util.Collection}" (java.lang.IllegalArgumentException). Message payload is of type: NullPayload (org.mule.api.MessagingException)
  org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor:32 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MessagingException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
java.lang.IllegalArgumentException: Object "org.mule.transport.NullPayload" not of correct type. It must be of type "{interface java.lang.Iterable,interface java.util.Iterator,interface org.mule.routing.MessageSequence,interface java.util.Collection}"
    at org.mule.util.collection.EventToMessageSequenceSplittingStrategy.split(EventToMessageSequenceSplittingStrategy.java:64)
    at org.mule.util.collection.EventToMessageSequenceSplittingStrategy.split(EventToMessageSequenceSplittingStrategy.java:25)
    at org.mule.routing.CollectionSplitter.splitMessageIntoSequence(CollectionSplitter.java:29)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

【问题讨论】:

    标签: unit-testing magento mule mule-studio munit


    【解决方案1】:

    一种方法是使用构造函数或属性/设置器自己构建对象。

    来自文档: http://mulesoft.github.io/magento-connector/2.1.2/java/com/magento/api/CustomerCustomerEntity.html

    <mock:then-return payload-ref="#[ent = new com.magento.api.CustomerCustomerEntity(); ent.email('test@test.com'); return ent;]"/>
    

    您还可以将这些对象创建为可重用的 spring bean 并从 MEL 中引用它们。

        <bean class="com.magento.api.CustomerCustomerEntity" id="myEntityWithEmail">
                <property name="email" value="test@test.com" />
        </bean>
    
        <mock:then-return payload-ref="#[app.registry.myEntityWithEmail]"/>
    

    更新后,我可以看到您使用了一个期望集合或可迭代的 foreach。您可以简单地在 MEL 中使用以下命令返回自定义对象的集合:[] 例如:

    #[ent = new com.magento.api.CustomerCustomerEntity(); ent.email('test@test.com'); return [ent];]
    

    更多关于 MEL 的信息在这里:https://developer.mulesoft.com/docs/display/current/Mule+Expression+Language+MEL

    或者你可以再次使用 Spring 返回一个列表:

    <util:list id="entities">
        <ref bean="myEntityWithEmail" />
    </util:list>
    

    【讨论】:

    • 嗨瑞恩,感谢您的回答。我已经给了第二种选择,但仍然没有运气。我看到的错误是:Object "org.mule.transport.NullPayload" not of correct type. It must be of type "{interface java.lang.Iterable,interface java.util.Iterator,interface org.mule.routing.MessageSequence,interface java.util.Collection}" (java.lang.IllegalArgumentException). Message payload is of type: NullPayload。我已经用新测试和我正在尝试测试的流程更新了我的原始问题。有什么想法吗?
    • 另外,当我尝试第一个选项时,我得到的错误是:Error: unable to resolve method: com.magento.api.CustomerCustomerEntity.email(java.lang.String) [arglength=1]
    • 更新了我的 answer.foreach 需要一个集合或可迭代等。所以返回一个集合:#[ent = new com.magento.api.CustomerCustomerEntity(); ent.email('test@test.com');返回 [en​​t];]
    • 我已经尝试过了(请参阅更新的答案),但仍然遇到类似的错误。如果可能,您能否展示如何使用 spring 方法返回列表?再次感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 2016-10-25
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    相关资源
    最近更新 更多