0

I am mocking a routing rule object by using the JSON list below that has an ID of type Routing_Rule__c. The Test is about creating some rules and setting some flags that would cause the lead object to be set with the right rule. The Lead & Routing_Rule__c have a lookup relationship.

@isTest
    private static void testSomeStuff() {

//My mock data for Routing Rule
        String routingRulesListJSON = '[{"attributes":{"type":"Routing_Rule__c"},"Id":"a2h55000000Hh9uAAC","Routing_Target_Type__c":"Some Target","Start_Date_Time__c":"2020-03-01","Type__c":"New Lead","External_Id__c":"32","End_Date_Time__c":"2021-03-30","Execution_Order__c":"1"}]';
         List<Routing_Rule__c> routingRuleList = (List<Routing_Rule__c>)JSON.deserialize(routingRulesListJSON, List<Routing_Rule__c>.class);
         Routing_Rule__c routingRule = routingRuleList.get(0);     
         MockProvider.routingRuleList = routingRuleList;
         LeadRouting_Engine_Helper.routingRulesSelectorObj = (RoutingRuleSelector)MockUtil.createMock(RoutingRuleSelector.class);

        Account testAccount = [SELECT Id,Client_Practice_Status__c FROM Account WHERE Client_Practice_Status__c='Active'];       

        Test.startTest();
            Map<String,Object> leadMap = new Map<String,Object>();       
            leadMap.put('Client__c',testAccount.Id);
            Lead testLead = TestDataFactory.createLead(leadMap,TRUE);
            Lead newLead = [SELECT Id, Routing_Rule__c FROM Lead WHERE Id =: testLead.Id];

            system.assertEquals(routingRule.Id, newLead.Routing_Rule__c, 'Some Assert');
        Test.stopTest();
    }

When I run the following, I get :

System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: a2h55000000Hh9u: []

The issue is on line:

Lead testLead = TestDataFactory.createLead(leadMap,TRUE);

Which leads to:

public static lead createLead(Map<String, Object> fieldVars, Boolean doDml) {
        Integer i = leadCounter++;
        Lead l = new Lead(
                Company = 'Company'+i
        );
        if (fieldVars != null) {
            for (String key : fieldVars.keySet()) {
                l.put(key, fieldVars.get(key));
            }
        }
        if (doDml) insert l;
        return l;
    }

Which leads further to the Lead Trigger that has a piece of code that sets:

Lead.Routing_Rule__c = some-rule.Id;

When I comment out this line in the trigger, I have a failed assert but test executes. However I want that field to be set on the lead object for proper test scenario. I have tried using the full test under sys admin context but same error.

The test executes just fine when I do SOQL to create the Routing_Rule__c and get that ID but I feel the issue is with the ID and the fact that there is a lookup relationship so the system kind of wants an inserted data for routing rule prior to lead object being saved.

Is there a way I can proceed in my test that uses mocking or will I have to use traditional SOQL inserts in cases like this?

  • You are mocking the Routing_rule__c but actually saving the Lead. So it's committing to the database with a lookup to a non-existing record. You prob need to test the logic that assigns the routing rule separately from the trigger, e.g. Calling the methods that the trigger would. When actually testing the trigger execution itself the records will have to be real. – packo_cz Mar 9 at 19:53
  • Thanks @packo_cz. I will look into that. But you are saying that I cannot save a lead object that has a Lookup Routing Rule ID that does not exist on the database? – kratos Mar 9 at 19:59
  • 1
    Exactly, mocking is really for when you don't have to go in the database. Say if you have your trigger logic in a separate handler/helper class, you can mock away the in-memory objects (pass in a "fake" trigger new list). But committing is committing, even in unit tests – packo_cz Mar 9 at 20:06
  • Thank you. Your advise worked perfectly ! – kratos Mar 9 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.