+ Start a Discussion
Marina KostopoulouMarina Kostopoulou 

Improve Apex Class with test with code coverage 44% to 75%

I don't have previous experience with Apex Code. I created an apex class that converts the Lead to Account and I write a simple test. With the current status my code coverage is 44% and I need at least 75% in order to deploy it in production. Can someone help me write that ?


Apex Class
Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
        
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead: LeadIds){
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead);                
                Leadconvert.setConvertedStatus('Qualified');
                MassLeadconvert.add(Leadconvert);
        }
        
        
        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }
}
Test Class
@isTest
private class AutoConvertLeads_Test{
  static testMethod void test_LeadAssign_UseCase1(){
    AutoConvertLeads obj01 = new AutoConvertLeads();
    AutoConvertLeads.LeadAssign(new List<Id>());
  }

}


 
Pranav ChitransPranav Chitrans
Hi Marina,

Please check below code snippet might help
@isTest
private class AutoConvertLeads_Test{
  private static void test_LeadAssign_UseCase1(){
    List<Lead> leads = setUpLeadData(5);
    List<Lead> lstOfLeadids = [Select id, // rest fields from lead];
    AutoConvertLeads.LeadAssign(lstOfLeadIds);
  }

  private static List<Lead> setUpLeadData(Integer noOfRecords) {
      
      List<Lead> listOfLeads = new List<Lead>();
      Lead obLead;
      for(Integer i=0; i<noOfRecords; i++) {
          obLead = new Lead();
          obLead.//create rest of the records//
          listOfLeads.add(obLead);
      }

      Insert listOfLeads;
      return listOfLeads;
  }

}

Thanks
Pranav​​​​​​​
Marina KostopoulouMarina Kostopoulou
I don't know why I get the following error. Can you help me with that please ?
User-added image
Pranav ChitransPranav Chitrans
Hi @Marina,

Chnage this to 
AutoConvertLeads.LeadAssign(lstOfLeadIds); --> AutoConvertLeads.LeadAssign(lstOfLeadIds.Id);
 
That was just a skeleton , u need to geet the converted lead as well
Pranav ChitransPranav Chitrans
you can ty something like
@isTest
Private class UnitTest_AutoConvert 
{
	Static TestMethod void AutoConvert()
	{
	    // Create the Lead object
	    Lead testLead = new Lead(
			FirstName='Demo 100800',
			LastName = 'Demo 100800 UnitTest',
			Status='Qualified',
			company='Lacosta',
			street='1211 E I-40 Service Rd',
			city=' Oklahoma City',
			state='OK'
  	    );
	    insert 	testLead;
	   
		test.StartTest();
			
			Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(testLead.Id);
            LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
            lc.setConvertedStatus(convertStatus.MasterLabel);
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            System.assert(lcr.isSuccess());

	    test.stopTest();
    }
}
Marina KostopoulouMarina Kostopoulou
This test is not working because the MasterLabel is not 'Qualified'. I changed the line 23 of your code with the hardcoded value 'Qualified'  so the test pass but again I get 0% code coverage at my class. Your test doesn't even call my AutoConvert class.