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
SF API 9SF API 9 

Auto Lead Conversion invokable method TEST Class

I used the below apex class as an invokable method to work with process builder. The code will fire when a certain field on the Lead record is populated. 

The class works perfectly:
Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead: LeadIds){
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead);                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion 
                MassLeadconvert.add(Leadconvert);
        }
        
        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }
}

The test class below (that i found online) is not giving me the coverage that I need.  Can someone help me??


@IsTest (SeeAllData=true) private class AutoConvertLead_Test{

    /* This is a basic test which simulates the primary positive case for the 
       Conversion method of a Lead. */

public static testMethod void myUnitTest() {

// create a Lead
Lead lead=new Lead(LastName='TestDoe',FirstName='TestJane',Company='TestUnknown',Status='Warm Prospect',Addy_Account_id__c='1234566');

insert lead;


Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(lead.id);
lc.setDoNotCreateOpportunity(true);
lc.setConvertedStatus('User');

Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
}}

 
Andrew GAndrew G
Hi
Try something like the below
@IsTest //(SeeAllData=true) - remove SeeAllData - not best practice
private class AutoConvertLead_Test{

    /* This is a basic test which simulates the primary positive case for the 
       Conversion method of a Lead. */

    @IsTest
    public static void myUnitTest() {

	// create a Lead
	Lead lead=new Lead(LastName='TestDoe',FirstName='TestJane',Company='TestUnknown',Status='Warm 	Prospect',Addy_Account_id__c='1234566');

	insert lead;

	//now invoke the class and method - not do a convert which the invocable method is meant to do
	Test.startTest();	
	AutoConvertLeads.LeadAssign(New Leadids[]{lead.Id});
	Test.stopTest();

	Lead converted = [SELECT Id, OwnerId, Status FROM Lead WHERE Id = :lead.Id];
	System.assertEquals('some expected user',converted.OwnerId);
	System.assertEquals('Qualified', converted.Status);
    }
}

basically your test class should invoke the class and methods defined in the class it is testing.  

Regards
Andrew