0

This question already has an answer here:

I have a working trigger that I'm starting to build my test class for. I've done a few lines and ran a test to make sure it's working as it should and everything seems okay, except the trigger itself is 0% coverage. Can someone help me figure out why?

Trigger:

trigger PartnerLeadAssignment on Lead (before insert) {

List<Lead> leadList = new List<Lead>();
for(Lead l : trigger.new){
    leadList.add(l);
    if(l.Company == null){
        l.Company = l.FirstName + ' ' + l.LastName;


        if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '0010H00002SuHKtQAN'){
            l.OwnerId = '0050H0000090O6HQAU'; 
            }

        if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '0010H00002Vv9gqQAB'){
            l.OwnerId = '0050H00000BwRULQA3'; 
            }    

        if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '0010H00002PZz8zQAD'){
            l.OwnerId = '0050H000009LmB3QAK'; 
            }

        if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '001i000001siaFLAAY'){
            l.OwnerId = '005i0000007xZy2AAE'; 
            }
    }
}

Test Class:

@isTest
public class PartnerLeadAssignmentTest {
static testMethod void insertLead(){


    List<Lead> leadList = new List<Lead>();
    Lead l = New Lead(Company = '', FirstName = 'First', LastName = 'Last', leadSource = 'Website', lead_SubType__c = 'Contact Requested');
    leadList.add(l);
    if(l.Referring_Brand_Id__c == '0010H00002SuHKtQAN'){ 

       insert l;

        l=[Select Company, FirstName, LastName, Referring_Brand_Id__c, LeadSource, OwnerId, RecordTypeId FROM Lead Where Id =:l.Id];
        System.assertEquals('0050H0000090O6HQAU', l.OwnerId); 
       }
    if(l.Referring_Brand_Id__c == '0010H00002Vv9gqQAB'){ 

        insert l;

        l=[Select Company, FirstName, LastName, Referring_Brand_Id__c, LeadSource, OwnerId, RecordTypeId FROM Lead Where Id =:l.Id];
        System.assertEquals('0050H00000BwRULQA3', l.OwnerId); 
      }


     }
}

The trigger is much longer than that, but just included a few lines for reference.

marked as duplicate by glls, Pranay Jaiswal, Mohith Shrivastava apex Aug 4 at 15:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

Your test class isn't performing any inserts. You're guarding your inserts with logic on a field that you don't populate and which is therefore null:

Lead l = New Lead(Company = '', FirstName = 'First', LastName = 'Last', leadSource = 'Website', lead_SubType__c = 'Contact Requested');
leadList.add(l);
if(l.Referring_Brand_Id__c == '0010H00002SuHKtQAN'){ 

Additionally, you should never have Ids hard-coded in your test classes. Because of test data isolation, your test code cannot see real data in your org, and Ids of metadata like record types and users will change from environment to environment. Your test class should always generate 100% of the data it requires in code.

  • I went a different route to this test class by inserting a new lead for each different scenario the trigger should pick up, but then ran into SOQL limit errors. I thought if I went the method originally listed above, I would get around those limits. Problems of a self learning developer. – tDactyl Jul 17 at 20:28
  • @tDactyl We've got some great unit test resources collected! See salesforce.stackexchange.com/questions/tagged/canonical-qa – David Reed Jul 17 at 20:28
  • @tDactyl - not to mention the hard-coded IDs in the trigger itself. You need to be querying for Accounts and Users based on some criteria that is application controlled (best set up in custom metadata or the like) – cropredy Jul 17 at 20:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.