function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ben Allington 7Ben Allington 7 

Can't get 100% code coverage with test class, stuck at 63%

Can't seem to get above 65% test coverage, I have marked where I am lacked coverage as far as I can tell it should cover this area but I am clearly missing something, any ideas?

MainClass
 
public class RelatedContantClass {
    public static void addRelatedContact (List <Lead> leadsFromTrigger) {
        
        //Create List for Opportunities to be updated from
        list <Opportunity> opportunityUpdates = new list <Opportunity> ();
        
        //For each Lead in the Trigger
        for (Lead currentLead : leadsFromTrigger){
            
            //Check if they have been converted
            if (currentLead.IsConverted == TRUE){
                
                //Check if the Lead Conversion included an Opportunity, then assign the contact to the Opportunity as Opportunity Contact Role Records
                if (currentLead.convertedOpportunityId != NULL) {
                    
           			list <Lead_Contact_Connector__c> LeadContactList = new list <Lead_Contact_Connector__c> ([Select Related_Contact__c, Contact_Role__c, Lead__c From Lead_Contact_Connector__c where Lead__c = :currentLead.Id]);	
                        list <OpportunityContactRole> ContactRoleNew = new list <OpportunityContactRole> ();
                            for (Lead_Contact_Connector__c CurrentContact: LeadContactList){
                                OpportunityContactRole OppCon = new OpportunityContactRole();
                                OppCon.ContactId = CurrentContact.Related_Contact__c;
                                OppCon.OpportunityId = currentLead.convertedOpportunityId;
                                OppCon.Role = CurrentContact.Contact_Role__c;
                                ContactRoleNew.add(OppCon);
                    }
                    
                    insert ContactRoleNew;

                }

            }
            
        }}}



TestClass

 
@isTest
private class RelatedContactClassTest {
    
    static testMethod void testCreateContactFromCandidate() {

        //Set up basic testing data.
        
       //Create a single Account for converting with an Opportunity
        Account aAccount = new Account (
            Name = 'First Test');
                
       //Create a single Contact for converting with an Opportunity
        Contact aContact = new Contact (
            FirstName = 'First Test',
            LastName = 'Lead',
            AccountId = aAccount.Id);
        
        //Create a single Lead for converting with an Opportunity
        Lead aLead = new Lead (
            FirstName = 'First Test',
            LastName = 'Lead',
            Company = 'Test Company X',
            Status = 'New',
          	LeadSource = 'Web');
        
      //create opportunity 
      Opportunity aOpp = new Opportunity(
      	Name='Test Opp'
      );
      //Create a single Related Contact for converting with an Opportunity
        Lead_Contact_Connector__c aRelatedContact = new Lead_Contact_Connector__c(
            Lead__c	 = aLead.Id,
            Contact_Role__c = 'Authorised Contact',
            Related_Contact__c = aContact.Id);
    

       //Create a single Lead for converting without an Opportunity
            Lead bLead = new Lead (
            FirstName = 'Second Test',
            LastName = 'Lead - No Opportunity',
            Company = 'Test Company Y',
            Status = 'New',
            LeadSource = 'Web');

       //Create a single Account for converting with an Opportunity
        Account bAccount = new Account (
            Name = 'First Test');
                
       //Create a single Contact for converting with an Opportunity
        Contact bContact = new Contact (
            FirstName = 'First Test',
            LastName = 'Lead',
            AccountId = bAccount.Id);
        
      //Create a single Related Contact for converting with an Opportunity
        Lead_Contact_Connector__c bRelatedContact = new Lead_Contact_Connector__c(
            Lead__c	 = bLead.Id,
            Contact_Role__c = 'Authorised Contact',
            Related_Contact__c = bContact.Id);            

        // Create 210 Leads for Converting with Opportunities
        List <Lead> leadList = new List <Lead> ();
        List <Account> AccountList = new List <Account> ();
        List <Contact> ContactList = new List <Contact> ();
        List <Lead_Contact_Connector__c> LCCList = new List <Lead_Contact_Connector__c> ();
        

        //Loop to create 210 Leads

        For (Integer i=0; i < 210; i++) {
            Account currentAccount = new Account ();
            currentAccount.Name = 'Test ' + i;            

            Contact currentContact = new Contact ();
            currentContact.FirstName = 'Test ' + i;
            currentContact.LastName = 'Lead';
            currentContact.AccountId = currentAccount.Id;
     		ContactList.add(currentContact);


            Lead currentLead = new Lead ();
            currentLead.FirstName = 'Test ' + i;
            currentLead.LastName = 'Lead';
            currentLead.Company = 'Test Company ' + i;
            currentLead.Status = 'New';
            currentLead.LeadSource = 'Web';
            
            Lead_Contact_Connector__c relatedContactLarge = new Lead_Contact_Connector__c ();
            relatedContactLarge.Lead__c	= currentLead.Id;
            relatedContactLarge.Contact_Role__c = 'Authorised Contact';
            relatedContactLarge.Related_Contact__c = currentContact.Id;
            
            LCCList.add(relatedContactLarge);
            leadList.add(currentLead);
            AccountList.add(currentAccount);
            }
        
        //Set the converted Lead Status as a Lead Status that exists in the org.
      LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];

        //_______________________________________________

        //Create each of the single leads and multi leads

        system.debug('single Lead: ' + aLead.FirstName + aLead.LastName);
        insert aLead;

        system.debug('single Lead: ' + bLead.FirstName + bLead.LastName);
        insert bLead;

        system.debug(leadList);
        database.insert(leadList);
        database.insert(LCCList);
        database.insert(ContactList);
		database.insert(AccountList);
     
        //_______________________________________________

        //Carry out the processes and testing logic

        //Test1
        //Verify that the single Lead converts with anis the converted Contact.
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(aLead.id);
            //This selects this first converted lead status possible.
        lc.setConvertedStatus(convertStatus.MasterLabel);
        lc.setOpportunityName('Test Opp 1');
            //This tests that the Lead conversion was successful.
       
        Database.LeadConvertResult lcr = Database.convertLead(lc);        
        System.assert(lcr.isSuccess());
        system.debug(lcr.isSuccess());
		System.assert(LCCList.size() != 0);
        
    }

 
Alain CabonAlain Cabon
Hello,

Where is your creation of a LeadStatus?

Objects that are used to manage your organization or metadata objects can still be accessed in your tests such as (page 558, Apex)
• User
• Profile
• Organization
• AsyncApexJob
• CronTrigger
• RecordType
• ApexClass
• ApexTrigger
• ApexComponent
• ApexPage​

There is not LeadStatus at all.
//Set the converted Lead Status as a Lead Status that exists in the org.
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];

http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/

Regards
Alain