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
Anshul BagdiyaAnshul Bagdiya 

How do I increase code coverage for the following apex class? Currently, its at 74%.

Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
            Database.LeadConvert Leadconvert = new Database.LeadConvert();
            Leadconvert.setLeadId(LeadIds[0]);
            LeadStatus Leads= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            Leadconvert.setConvertedStatus(Leads.MasterLabel);
            Leadconvert.setDoNotCreateOpportunity(TRUE);
            Database.LeadConvertResult Leadconverts = Database.convertLead(Leadconvert);
            System.assert(Leadconverts.isSuccess());
   }
}
Ruwantha  LankathilakaRuwantha Lankathilaka
Try following code it should give you 100% coverage 
 
@IsTest(SeeAllData = true)
public with sharing class ApexTest_AutoConvertLeads {

    static testMethod void testLeads(){


        Map<Id,Lead> idLeadMap = new Map<Id, Lead>([SELECT Id FROM Lead LIMIT 1000]);

        AutoConvertLeads.LeadAssign(new List<Id>(idLeadMap.keySet()));
    }
}

If this helps you please mark it as the best answer so it could help the community in many ways.
Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm

Please check below for sample test classes
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html


Please try below test class
@IsTest
public with sharing class AutoConvertLeadsTest {

    static testMethod void testLeads(){

        Lead newLead = new Lead() ;
        newLead.FirstName = 'Cole';
        newLead.LastName = 'Swain';
        newLead.Company = 'BlueWave';
        newLead.Status = 'contacted';	
		insert newLead;
		
		List<Id> LeadIds = new List<Id>();
		LeadIds.add(newLead.id);	
		
		AutoConvertLeads.LeadAssign(LeadIds);
		
    }
}

NOTE:- Try to avoid to use SeeAllData = true

Let us know if this will help you