+ 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
    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();
        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
Test Class
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
private class AutoConvertLeads_Test{
  private static void test_LeadAssign_UseCase1(){
    List<Lead> leads = setUpLeadData(5);
    List<Lead> lstOfLeadids = [Select id, // rest fields from lead];

  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//

      Insert listOfLeads;
      return listOfLeads;


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
Private class UnitTest_AutoConvert 
	Static TestMethod void AutoConvert()
	    // Create the Lead object
	    Lead testLead = new Lead(
			FirstName='Demo 100800',
			LastName = 'Demo 100800 UnitTest',
			street='1211 E I-40 Service Rd',
			city=' Oklahoma City',
	    insert 	testLead;
			Database.LeadConvert lc = new database.LeadConvert();
            LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
            Database.LeadConvertResult lcr = Database.convertLead(lc);

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.