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
Paul ZamskyPaul Zamsky 

Need Help Increasing Test Code Coverage

Hi Guys,

I need help increasing my Test Code Coverage for an apex trigger I have. I'm new to Apex and I'm learning on the spot.  I'm at 69% with this but need to get it over 75%. The higher the better.

Thanks

Paul


Trigger

trigger Lead_To_Candidate_2 on Lead (after insert, after update)
{
    set<string> phoneNumbers = new set<string>();
    String disposition;
    String email;
    String firstname;
    String lastname;
      
    for(Lead obj: Trigger.new)
    {
        if (obj.disposition__c != NULL  )
        {
            phoneNumbers.add(obj.Phone);
            disposition = obj.disposition__c;
            firstname = obj.firstName;
            lastname = obj.lastName;
            email = obj.email;
         
                }
    }
 
    List<Candidate_Non_Member__c> candidate = [SELECT Id, Formated_Phone_Number__c, Called__c, Latest_Dispostion__c, Contact_Email__c, Contact_First_Name__c, Contact_Last_Name__c FROM Candidate_Non_Member__c WHERE Formated_Phone_Number__c IN :phoneNumbers];
    List<Candidate_Non_Member__c> candidateUpdateList = new List<Candidate_Non_Member__c>();
 
    for(Candidate_Non_Member__c c: candidate)
    {
        c.Called__c = 1; //this is if called__c is a number field. If it is a text field, the line should be c.Called__c = '1';
        c.Latest_Dispostion__c = disposition;
        c.Contact_Email__c = email;
        c.Contact_First_Name__c = firstname;
        c.Contact_Last_Name__c = lastname;
      
        candidateUpdateList.add(c);
    }
    if(candidateUpdateList.size() > 0)
    {
        update candidateUpdateList;
    }
}




Test Class:

@isTest

/*
* Tests
*/


/*
* This is the basic test
*/

public with sharing class TestLead_To_Candidate_2 {


     public static final String DISPOSITION = 'Registered';
     public static final String COMPANY = 'Test Company';
     public static final String EMAIL = 'tuser15@salesforce.com';
     public static final String FIRSTNAME = 'John';
     public static final String LASTNAME = 'Doe';
     public static final String PHONE = '+12125555403';

    static testMethod void basicTest() {

        // Create dummy lead
        Lead testLead = new Lead(Company='Test Company',FirstName='John',LastName='Doe', Email='tuser15@salesforce.com', disposition__c = 'Registered', Phone = '+12125555403');
        insert testLead;
  
            
        List<Candidate_Non_Member__c> Candidate = [select id, Latest_Dispostion__c, Contact_Email__c, Called__c, Formated_Phone_Number__c, Contact_First_Name__c FROM Candidate_Non_Member__c where Formated_Phone_Number__c=:testLead.Phone];
   
        for (Candidate_Non_Member__c c : Candidate){
        //   System.assertEquals(aLead.ActivityHistories[0].subject, 'Email: '+SUBJECT);
      
            System.assertEquals(testLead.Email, c.Contact_Email__c);
            System.assertEquals(testLead.FirstName, c.Contact_First_Name__c);
            System.assertEquals(c.Called__c, 1);
      
          
          
         
                         
        }
      
      

      

    }


}
Ramu_SFDCRamu_SFDC
Set disposition as null and insert another lead in another test method. Do a test for bulk data insert, and update. 

Karan Khanna 6Karan Khanna 6
Run test class in Developer console and it will highlight the lines which are yet to be covered, then it would be easy to suggest.
lakslaks
Hi Paul,

In your trigger, the candidate list might not get populated while executing the test class. And hence the code using the list also may not be covered reducing your code coverage.

This is because in your test class you have not inserted any record for Candidate_Non_Member__c where Formated_Phone_Number__c = '+12125555403'.

You would have to insert 1 or more records for Candidate_Non_Member__c object (where atleast 1 record matches the criteria you are querying for in the trigger) before you create the dummy lead and insert. 
This will ensure that the query [SELECT Id, Formated_Phone_Number__c, Called__c, Latest_Dispostion__c, Contact_Email__c, Contact_First_Name__c, Contact_Last_Name__c FROM Candidate_Non_Member__c WHERE Formated_Phone_Number__c IN :phoneNumbers];
returns records and populates the candidate list. This will in turn cause the rest of the code using the list to be covered as well.

Hope this helps.

Lakshmi.