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?