在流中模拟多个连接器

Mocking Multiple connectors in the flow

我有一个流程,其中我有一个连接器来查询和连接器在 Salesforce 中创建记录。我是 Mulesoft 和 Munit 测试的新手。我刚刚使用一个 Salesforce 连接器为流程创建了一个简单的 Munit 测试。只是尝试做同样的事情,但 运行 会用两个 mock

来处理 Munit 测试

使用两个 Salesforce 连接器的流程

    <sub-flow name="salesforce-Quote-create-flow" doc:id="310fe17f-64b3-448e-8c13-d5994352c3f5" >
        <json-logger:logger doc:name="Begin Flow (Info)" doc:id="92cbe814-9391-4ef0-900b-33c72643f3d7" config-ref="JSON_Logger_Config" message="Starting salesforce-Quote-create-flow" category="${jsonlogger.category}">
            <json-logger:content ><![CDATA[#[import modules::JSONLoggerModule output application/json ---
{
    name: payload.quoteID,
    opportunityId : payload.opportunityId,
}]]]></json-logger:content>
        </json-logger:logger>
        <set-variable value="#[payload]" doc:name="Set the input Payload" doc:id="6b46a184-fabb-4063-a1b8-a074622f73dc" variableName="inputPayload"/>
        <salesforce:query doc:name="Query Existing Quote" doc:id="19c396a3-8656-4600-beba-9e011e78d2b4" config-ref="Salesforce_Config">
            <salesforce:salesforce-query ><![CDATA[Select ID,Opportunity.recordTypeID,Contact_Information__c, Executive_Summary_checkbox__c, Milestones_checkbox__c, Gantt_Chart__c,
 Shipment_Schedule__c, Timeline_Tool__c, Product_Description__c, Materials_and_Pricing__c,
 To_accept_this_statement_of_work__c, Animal_Health_Policy__c, Cancelation_Policy__c, 
 Fees_and_Invoicing__c, Terms_and_Conditions__c, Signature__c, Customer_Signature__c, Executive_Summary__c, 
 (SELECT id,Quote__c FROM Quote_Sent_To__r) FROM Quote WHERE OpportunityId= ':opportunityId' Order by createddate desc limit 1  
        ]]></salesforce:salesforce-query>
            <salesforce:parameters ><![CDATA[#[output application/java
---
{
    "opportunityId" : vars.inputPayload.opportunityId
}]]]></salesforce:parameters>
        </salesforce:query>
        <set-variable value="#[payload]" doc:name="Set Variable exisingQuote" doc:id="9e591720-7e19-4f8f-b86f-98b05ee896f7" variableName="existingQuote"/>
        <ee:transform doc:name="Map the Request Body to Create New Quote" doc:id="f3d95e54-95a5-466d-b3c3-7d3323aef6b0">
                    <ee:message>
                        <ee:set-payload><![CDATA[%dw 2.0
output application/java
---
if(payload[0].Id != null)
[{
Name : vars.inputPayload.name,
OpportunityId: vars.inputPayload.opportunityId,
Pricebook2Id: vars.inputPayload.pricebook2d,
AX_Instance__c : vars.inputPayload.axInstance,
Multiline_Discount__c : vars.inputPayload.multilineDiscount,
Total_Discount__c: vars.inputPayload.totalDiscount,
Customer_Signature__c  : payload[0].Customer_Signature__c as Boolean,
Executive_Summary__c  : payload[0].Executive_Summary__c
}]
else
[{
Name : vars.inputPayload.name,
OpportunityId: vars.inputPayload.opportunityId,
Pricebook2Id: vars.inputPayload.pricebook2d,
AX_Instance__c : vars.inputPayload.axInstance,
Multiline_Discount__c : vars.inputPayload.multilineDiscount,
Total_Discount__c: vars.inputPayload.totalDiscount,
    }]]]></ee:set-payload>
                    </ee:message>
                </ee:transform>
        <set-variable value="#[payload[0]]" doc:name="Set Variable" doc:id="e31add26-4221-4004-9c19-b5d35d51a565" variableName="quoteBody"/>
        <json-logger:logger doc:name="After Map Request Body" doc:id="381c37f1-7dfa-4085-a294-7571424bdda2" config-ref="JSON_Logger_Config" message="After Building the Request body for the in salesforce-Quote-create-flow" tracePoint="AFTER_TRANSFORM" priority="DEBUG" category="${jsonlogger.category}">
            <json-logger:content ><![CDATA[#[import modules::JSONLoggerModule output application/json ---
{
    payload: payload
}]]]></json-logger:content>
        </json-logger:logger>
        <salesforce:create doc:name="Create Quote Record" doc:id="99e7fd61-af4f-4bed-bcb6-4f86e91c8a4b" config-ref="Salesforce_Config" type="Quote"/>
        <json-logger:logger doc:name="After Create Request to Salesforce (INFO)" doc:id="113e7428-eb0b-48b9-b007-afc7f2e44f0d" config-ref="JSON_Logger_Config" message="After Salesforce Create Operation in salesforce-Quote-create-flow" category="${jsonlogger.category}">
            <json-logger:content ><![CDATA[#[import modules::JSONLoggerModule output application/json ---
{
    success: payload.successful
}]]]></json-logger:content>
        </json-logger:logger>
    
</sub-flow>

Munit 测试如下

    <munit:test name="salesforce-sys-implementation-test-suite-salesforce-Quote-create-flowTest" doc:id="801d6bae-4579-4e52-8405-871c30fc1a98" description="Test">
        <munit:behavior>
            <munit-tools:mock-when doc:name="Mock when" doc:id="fe9d022f-e6c8-47a4-9a28-b1d50e72eabd" processor="salesforce:query">
                <munit-tools:with-attributes >
                    <munit-tools:with-attribute whereValue="Query Existing Quote" attributeName="doc:name" />
                    <munit-tools:with-attribute whereValue="19c396a3-8656-4600-beba-9e011e78d2b4" attributeName="doc:id" />
                </munit-tools:with-attributes>
            </munit-tools:mock-when>
            <munit-tools:mock-when doc:name="Mock when" doc:id="f1a78a02-6664-4bde-ae98-17c81eb1b181" processor="salesforce:create">
                <munit-tools:with-attributes >
                    <munit-tools:with-attribute whereValue="Create Quote Record" attributeName="doc:name" />
                    <munit-tools:with-attribute whereValue="99e7fd61-af4f-4bed-bcb6-4f86e91c8a4b" attributeName="doc:id" />
                </munit-tools:with-attributes>
            </munit-tools:mock-when>
        
</munit:behavior>
        <munit:execution>
            <munit:set-event doc:name="Set Event" doc:id="8c0f220b-d687-4de5-ba72-7d08fb75cc17">
                <munit:payload value="#[MunitTools::getResourceAsString('samples/quote.json')]" encoding="UTF-8" mediaType="application/json" />
            </munit:set-event>
            <flow-ref doc:name="Flow Reference" doc:id="e312ebeb-dbe5-4728-b13d-8e43d916bd84" name="salesforce-Quote-create-flow"/>
        </munit:execution>
        <munit:validation >
            <set-variable value="#[output application/json --- readUrl('classpath://samples/quoteResponse.json', 'application/json')]" doc:name="Set Variable" doc:id="1cec72cf-dea7-4a24-935b-aa0d497930d1" variableName="expectedPayload"/>
            <munit-tools:assert-that doc:name="Assert that Not Null Response" doc:id="9a66b251-8dd6-4f5a-980d-0f607de91863" is="#[MunitTools::notNullValue()]" expression="#[vars.expectedPayload.Id]"/>
        </munit:validation>
    </munit:test>

提示错误

org.mule.runtime.api.el.ExpressionExecutionException: You called the function 'Value Selector' with these arguments: 
  1: String ("Q138446")
  2: Name ("Id")

But it expects one of these combinations:
  (Array, Name)
  (Array, String)
  (Date, Name)
  (DateTime, Name)
  (LocalDateTime, Name)
  (LocalTime, Name)
  (Object, Name)
  (Object, String)
  (Period, Name)
  (Time, Name)

4| if(payload[0].Id != null)
      ^^^^^^^^^^^^^
Trace:
  at anonymous::main (line: 4, column: 4)

创建连接器之前的转换出错。我如何在那里设置有效负载并在我的流程中模拟创建连接器。非常感谢任何帮助

看来您正在为模拟设置条件,但实际上并未设置值来替换执行。

请参阅 documentation 中的示例:



<munit-tools:mock-when processor="http:request">
   <munit-tools:with-attributes>
       <munit-tools:with-attribute attributeName="method" whereValue="#['POST']"/>
   </munit-tools:with-attributes>
   <munit-tools:then-return>
       <munit-tools:payload value="#['mockPayload']"/>
   </munit-tools:then-return>
</munit-tools:mock-when>

请注意,在 <munit-tools:with-attributes> 之后有一个 <munit-tools:then-return> 元素用于设置模拟负载。

阅读上面的文档 link 以了解如何使用模拟及其具有的各种选项。