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
Andrey BolkonskyAndrey Bolkonsky 

Convert Lead Test Class is Not Working

I've made a class that converts leads and also a Test Data Factory class that makes test leads.

However My below test class is not working. When I used System.Assert(), I get the error System.AssertException: Assertion Failed

When I used System.AssertEquals(Lead1.IsConverted, TRUE) I get the error System.AssertException: Assertion Failed: Expected: false, Actual: true

What am I doing wrong?
 
@isTest
global class ConvertLeadTest {

    @isTest
    static void convertOneLead(){
        
        Lead[] TestLeads = TestDataFactory2.getTestLeads(1);
        List<Id> IdsIn = new List<Id>();
        for(Lead Lead : TestLeads){
            IdsIn.add(Lead.Id);
        }
        
        insert TestLeads;
        
        Lead Lead1 = TestLeads[0];
       
        ConvertLead.LeadsToConvert(IdsIn);
        
        System.assert(Lead1.IsConverted);
    }
    
}

 
Best Answer chosen by Andrey Bolkonsky
Maharajan CMaharajan C
Hi Andrey,

You can keep the  insert the Test Leads in the Test Class but it should be above the for loop. Inside the loop you are accessing the Id so it's need to inserted before loop. 

You need to query the lead record to see the latest changes in test class after some logic execution. 
 
@isTest
global class ConvertLeadTest {

    @isTest
    static void convertOneLead(){
        
        Lead[] TestLeads = TestDataFactory2.getTestLeads(1);
        insert TestLeads; 

        List<Id> IdsIn = new List<Id>();
        for(Lead Lead : TestLeads){
            IdsIn.add(Lead.Id);
        }
        
        Lead Lead1 = TestLeads[0];
       
        ConvertLead.LeadsToConvert(IdsIn);
		
		system.debug(' Lead1.IsConverted --> '+ Lead1.IsConverted);
		Lead ld = [Select Id,IsConverted from Lead where Id =:  Lead1.Id];
		system.debug(' ld.IsConverted --> ' + ld.IsConverted);
        
        System.assert(ld.IsConverted);
    }
    
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Andrey,

Execute the below code and see the debugs. If you are not getting isconverted as true in debug line below the query then your actual class is not converting the test lead.
 
@isTest
global class ConvertLeadTest {

    @isTest
    static void convertOneLead(){
        
        Lead[] TestLeads = TestDataFactory2.getTestLeads(1);
        List<Id> IdsIn = new List<Id>();
        for(Lead Lead : TestLeads){
            IdsIn.add(Lead.Id);
        }
        
        insert TestLeads;
        
        Lead Lead1 = TestLeads[0];
       
        ConvertLead.LeadsToConvert(IdsIn);
		
		system.debug(' Lead1.IsConverted --> '+ Lead1.IsConverted);
		Lead ld = [Select Id,IsConverted from Lead where Id =:  Lead1.Id];
		system.debug(' ld.IsConverted --> ' + ld.IsConverted);
        
        System.assert(ld.IsConverted);
    }
    
}

Thanks,
Maharajan.C
Andrey BolkonskyAndrey Bolkonsky
Maharajan,

it seems since I tried to insert the Test Leads in the Test Class, it did not work. However Once I moved the Insert statement to the end of the Test Data Class it worked.

Can you please tell me why is it that the first debug statement is showing false, but the debug statment after the Query is showing true?
Maharajan CMaharajan C
Hi Andrey,

You can keep the  insert the Test Leads in the Test Class but it should be above the for loop. Inside the loop you are accessing the Id so it's need to inserted before loop. 

You need to query the lead record to see the latest changes in test class after some logic execution. 
 
@isTest
global class ConvertLeadTest {

    @isTest
    static void convertOneLead(){
        
        Lead[] TestLeads = TestDataFactory2.getTestLeads(1);
        insert TestLeads; 

        List<Id> IdsIn = new List<Id>();
        for(Lead Lead : TestLeads){
            IdsIn.add(Lead.Id);
        }
        
        Lead Lead1 = TestLeads[0];
       
        ConvertLead.LeadsToConvert(IdsIn);
		
		system.debug(' Lead1.IsConverted --> '+ Lead1.IsConverted);
		Lead ld = [Select Id,IsConverted from Lead where Id =:  Lead1.Id];
		system.debug(' ld.IsConverted --> ' + ld.IsConverted);
        
        System.assert(ld.IsConverted);
    }
    
}

Thanks,
Maharajan.C
This was selected as the best answer
Andrey BolkonskyAndrey Bolkonsky
Thanks, I didn't know it worked that way.