无法使 Salesforce 连接器的 Mock 在 Munit 测试中工作

Cannot make the Mock for the Salesforce Connectors work in the Munit Tests

我一直在努力为我创建的流进行 Munit 测试。 Flow 有几个用于查询、创建的 Salesforce 连接器。因此,在我的 Munit 测试中,我添加了 Mocks,但似乎我遗漏了一些东西。流程如下

<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:behavior>
        <munit-tools:mock-when doc:name="Mock when" doc:id="5bfeb53b-2da1-45f9-bad3-67cf4d658fc2" 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:then-return >
                <munit-tools:payload value="#[output application/json --- readUrl('classpath://samples/quoteQueryResp.dwl')]" mediaType="application/java" encoding="UTF-8" />
            </munit-tools:then-return>
        </munit-tools:mock-when>
        <munit-tools:mock-when doc:name="Mock when" doc:id="2d8c3c7b-04fd-4119-a535-d8b72c3af685" 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:then-return >
                <munit-tools:payload value="#[output application/json --- readUrl('classpath://samples/resp.dwl')]" mediaType="application/java" encoding="UTF-8" />
            </munit-tools:then-return>
    </munit-tools:mock-when>
 </munit:behavior>
    <munit:execution>
        <munit:set-event doc:name="Set Event" doc:id="b09dba54-76cc-401b-85aa-5657849861c4">
            <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="875fa5b7-290d-4d3f-abfe-3098bf6c7ce1" 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="6fd61167-5025-4da3-8277-c9303ba9da57" variableName="expectedPayload"/>
        <munit-tools:assert-that doc:name="Assert that Not Null Response" doc:id="dc4d4008-83bd-4da6-8773-fc4aa0285d0c" is="#[MunitTools::notNullValue()]" expression="#[vars.expectedPayload.Id]"/>
    </munit:validation>
</munit:test>

quoteQueryResp.dwl 文件如下

[
  {
    "Product_Description__c": "false",
    "Shipment_Schedule__c": "false",
    "Executive_Summary_checkbox__c": "false",
    "type": "Quote",
    "Milestones_checkbox__c": "false",
    "Terms_and_Conditions__c": "true",
    "Timeline_Tool__c": "false",
    "Signature__c": "false",
    "Executive_Summary__c": null,
    "Quote_Sent_To__r": null,
    "Contact_Information__c": "true",
    "Gantt_Chart__c": "false",
    "Animal_Health_Policy__c": "true",
    "Opportunity": {
      "RecordTypeId": "012U0000000QtmpIAC",
      "Id": null,
      "type": "Opportunity"
    },
    "Materials_and_Pricing__c": "false",
    "Cancelation_Policy__c": "true",
    "To_accept_this_statement_of_work__c": "true",
    "Customer_Signature__c": "false",
    "Id": "0Q02D0000005vu7SAA",
    "Fees_and_Invoicing__c": "true"
  }
]

但我一直收到错误

org.mule.runtime.api.el.ExpressionExecutionException: You called the function 'Value Selector' with these arguments: 
1: String ("[")
2: Name ("Id")
   
4| if(payload[0].Id != null)

   ^^^^^^^^^^^^^
Trace:
at anonymous::main (line: 4, column: 4)

不确定我错过了什么。非常感谢任何帮助这是我第一次创建 Mulesoft 应用程序

quoteQueryResp.dwl 似乎真的是一个 JSON 文档,它可以在 DataWeave 脚本中运行,但缺少 headers。然后您在表达式而不是脚本中向 JSON 添加输出,然后将模拟的媒体类型设置为 application/java。也许所有这些转换都让 DataWeave 误以为有效载荷是一个字符串。尝试将输出移动到脚本并更改媒体类型以匹配脚本中定义为输出的内容。请参阅 MUnit examples in the documentation 指导。

您是否正在尝试将模拟 .dwl 解读为 java 或 json?尝试在两个地方提供相同的媒体类型。或者更好的是,删除 readUrl 之前的 output application/json --- 并只在 mediaType 属性中给出输出类型。像这样

<munit-tools:payload value="#[readUrl('classpath://samples/quoteQueryResp.dwl')]" mediaType="application/java" encoding="UTF-8" />