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
Natasha AliNatasha Ali 

Hi, Below is my new autoconvert lead trigger but I need to write a new test class for it and I'., unsure as to how I would do that in the existing code:

The Apex Trigger: 

Trigger AutoConvert on Lead (after insert) {
     LeadStatus convertStatus = [
          select MasterLabel
          from LeadStatus
          where IsConverted = true
          limit 1
     ];
     List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

     for (Lead lead: Trigger.new) {
          if (!lead.isConverted && lead.Company == null) {
               Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Name;
               
               lc.setLeadId(lead.Id);
               lc.setDoNotCreateOpportunity(true);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               
               leadConverts.add(lc);
          }
     }

     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}


The test class for the old trigger:

@IsTest
private class AutoConverter_Test {
    private static Integer LEAD_COUNT = 0;
    
    private static Lead createLead() {
        LEAD_COUNT += 1;
        return new Lead(
            FirstName = '_unittest_firstname_: ' + LEAD_COUNT,
            LastName = '_unittest_lastname_: ' + LEAD_COUNT,
            Company = '_unittest_company_: ' + LEAD_COUNT,
            Status = 'Inquiry'
        );
    }
    
    public static void makeFreeTrial(Lead lead) {
        lead.Company = null;
    }
    
    public static List<Lead> fetchLeads(Set<Id> ids) {
        return [
            select isConverted
            from Lead
            where Id in :ids
        ];
    }
    
    public static testMethod void trialConvert() {
        Lead testLead = createLead();
        makeFreeTrial(testLead);

        Test.startTest();
        
        insert testLead;
        
        Test.stopTest();
        
        List<Lead> results = fetchLeads(new Set<Id>{testLead.Id});
        
        System.assertEquals(1, results.size(), 'Did not get the right number of leads back');
        System.assert(results.get(0).isConverted, 'The lead should have been converted since it was a "Free Trail"');
    }

    public static testMethod void nonTrialNoConvert() {
        Lead testLead = createLead();
        
        Test.startTest();
        
        insert testLead;
        
        Test.stopTest();
        
        List<Lead> results = fetchLeads(new Set<Id>{testLead.Id});
        
        System.assertEquals(1, results.size(), 'Did not get the right number of leads back');
        System.assert(!results.get(0).isConverted, 'The lead should not have been converted since it was not a "Free Trail"');
    }
    
    public static testMethod void bulkTest() {
        List<Lead> shouldBeConverted = new List<Lead>();
        List<Lead> shouldNotBeConverted = new List<Lead>();
    
        for (Integer i = 0; i < 50; i++) {
            Lead testLeadNonConvert = createLead();
            Lead testLeadConvert = createLead();
            makeFreeTrial(testLeadConvert);
            
            shouldBeConverted.add(testLeadConvert);
            shouldNotBeConverted.add(testLeadNonConvert);
        }
        
        List<Lead> toInsert = new List<Lead>();
        toInsert.addAll(shouldBeConverted);
        toInsert.addAll(shouldNotBeConverted);
        
        Test.startTest();
        
        insert toInsert;
        
        Test.stopTest();
        
        Map<Id, Lead> expectedConversions = new Map<Id, Lead>(shouldBeConverted);
        Map<Id, Lead> expectedNonConversions = new Map<Id, Lead>(shouldNotBeConverted);
        
        Set<Id> leadIds = new Set<Id>();
        leadIds.addAll(expectedConversions.keySet());
        leadIds.addAll(expectedNonConversions.keySet());
        
        for (Lead result: fetchLeads(leadIds)) {
            if (expectedConversions.containsKey(result.Id)) {
                System.assert(result.isConverted, 'This lead should have been converted ' + result);
                expectedConversions.remove(result.Id);
            } else if (expectedNonConversions.containsKey(result.Id)) {
                System.assert(!result.isConverted, 'This lead should not have been converted ' + result);
                expectedNonConversions.remove(result.Id);
            } else {
                System.assert(false, 'We got a Lead we did not expect to get back ' + result);
            }
        }
        
        System.assert(expectedConversions.isEmpty(), 'We did not get back all the converted leads we expected');
        System.assert(expectedNonConversions.isEmpty(), 'We did not get back all the non converted leads we expected');
    }
}


Many Thanks!! :)

Natasha

Best Answer chosen by Natasha Ali
Avishek Nanda 14Avishek Nanda 14
Hey  Natasha,

Just Create a Lead in Your test method and Convert that. Something Like below 
@IsTest (SeeAllData=true) 

private class AutoConvertLead{

private static testMethod void myUnitTest() {

// create a Lead
Lead lead=new Lead(LastName='Doe',FirstName='John',Company='Test',Status='Inquiry');

insert lead;                

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

Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
}}
Mark this as the best answer if this resolved your query.