Search code examples
mulemule-studiomulesoftmule4munit

Mocking a connector throws error like Element not defined in Mule Registery


I am creating the Munit tests and mocking the salesforce connector for the query. The Query response in the flow returns the payload in application/java type. So below is the Mock the setting

enter image description here

<munit:test name="salesforce-sys-implementation-test-suite-salesforce-Quote-create-flowTest" doc:id="40f70f6a-3453-4a8b-9ed8-673e4796c0be" description="Test">
    <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="[{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}]" mediaType="application/java" encoding="UTF-8" />
            </munit-tools:then-return>          
    </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>

It throws error when I run the test

Element '[{Product_Description__c=false,....., Opportunity={RecordTypeId=012U0000000QtmpIAC, Id=null, type=Opportunity}}]' is not defined in the Mule Registry

How to set the response payload on the Mock which is the Query response here which of application /java type


Solution

  • It looks like you missed the # before the brackets at the value to indicate it is an expression. I'm guessing that without considering an expression MUnit tries to use the value as a literal id to search in the Mule Registry, but that is not something you need to care about. Just add the missing # and try again.

    Example:

    <munit-tools:payload value="#[output application/java
     --- {Product_Description__c:false,...}]" />