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
rahul Shukla 37rahul Shukla 37 

Lead conversion forTest class

public class cfg_LeadUtility {
     @InvocableMethod(label='Convert the Lead' description='Convert the lead by LeadId and returns True/False as success/failures' category='Lead')
    public static List<String> ConvertLead(List<ID> leadIds) {
        List<String> convertedLeads= new List<String>();
        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        for (String leadId: leadIds) {
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(leadId);
            lc.setConvertedStatus(convertStatus.MasterLabel);   
                    
            Database.LeadConvertResult lcr ;
            try{
                lcr = Database.convertLead(lc);
                system.debug('*****lcr.isSuccess()'+lcr.isSuccess());            
                convertedLeads.add(LeadId);
            }
            catch(exception ex){
                system.debug('***LEAD NOT CONVERTED**');           
            }
        }
        
        return convertedLeads;
    }   
}
i wants to test this class as per below Test class but not covering the code please anyone help me to find the issue in Test class
@isTest  
private class cfg_LeadUtilityTest {  
      
    static testMethod void testLeadConv() {  
          
        //Lead objLead = new Lead( FirstName = 'Test', LastName = 'Sample', Company = 'Testing Sample Co' );  
        Lead objLead = utilityHelperTest.createLead();
        insert objLead;  
          
        Database.LeadConvert lc = new database.LeadConvert();  
        lc.setLeadId( objLead.Id );  
        //lc.setDoNotCreateOpportunity( true );  
        lc.setConvertedStatus( 'Closed - Converted' );  
          
        Database.LeadConvertResult lcr = Database.convertLead(lc, false);  
          
        system.debug( 'Errors are ' + lcr.getErrors() );  
          
        system.assert( lcr.isSuccess() );  
          
    }  
  
}
 
SubratSubrat (Salesforce Developers) 
Hello Rahul ,

Here's an example of how you could modify the cfg_LeadUtilityTest class to test the cfg_LeadUtility class:
 
@isTest  
private class cfg_LeadUtilityTest {  
    
    static testMethod void testLeadConv() {  
        // Create a new lead record
        Lead objLead = new Lead( FirstName = 'Test', LastName = 'Sample', Company = 'Testing Sample Co' );  
        insert objLead;
        
        // Call the ConvertLead method in cfg_LeadUtility class
        List<String> convertedLeads = cfg_LeadUtility.ConvertLead(new List<ID>{objLead.Id});
        
        // Assert that the lead was successfully converted
        System.assertEquals(1, convertedLeads.size());
        System.assertEquals(objLead.Id, convertedLeads[0]);
        
        Lead convertedLead = [SELECT Id, IsConverted, Status FROM Lead WHERE Id = :objLead.Id];
        System.assertEquals(true, convertedLead.IsConverted);
        System.assertEquals('Closed - Converted', convertedLead.Status);
    }
}


This test class creates a new lead record, calls the ConvertLead method in cfg_LeadUtility class, and then asserts that the lead was successfully converted by checking that its status is updated to the converted status and that it is no longer a lead record.

Hope it helps !