+ Start a Discussion
adambreen.ax1546adambreen.ax1546 

How to properly test a Lead Conversion (trigger)?

I have this trigger defined:

 

trigger convertLeadToImplementationOpportunity on Lead (before update) {
    for (Lead lead : Trigger.new){

      if(lead.Status == 'Qualified' && !lead.isConverted && lead.isApproved__c == true){
          Database.LeadConvert leadConvert = new database.LeadConvert();
          LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];

          leadConvert.setConvertedStatus(convertStatus.MasterLabel); // ("Closed - Converted");
          leadConvert.setLeadId(lead.id);

          Database.LeadConvertResult leadConvertResult = Database.convertLead(leadConvert);
          System.assert(leadConvertResult.isSuccess());
      }
    }
}

 

When I execute the main trigger code anonymously via the IDE, it succeeds.


I have this (failing) test defined for the trigger:

 

@isTest
privateclass tests {
static testMethod void approvedLeadShouldConvertToImplementationOpportunity() {

      Lead testLead = new Lead(FirstName='John', LastName='Smith', Status='Qualified');
      insert testLead;

      test.startTest();

       testlead.isApproved__c = true;
       update testlead;  //trigger convertLeadToImplementationOpportunity

       test.stopTest();

       Lead isConverted = [SELECT id, isConverted FROM Lead WHERE Id=:testlead.id];
       System.assert(isConverted.isConverted);    // fails - i.e. isConverted == false
    }
}

 

Can anyone please tell me why the assert is failing? I'm quite new to Apex, so forgive any obvious stupidity.


Thanks!

Adam Breen

 

Best Answer chosen by Admin (Salesforce Developers) 
SFAdmin5SFAdmin5

yeah that trigger needs to be re-written to bulkify it. but it will work in the ui  also it needs to be after update not before update.  that won't work with before update i don't think.

 

this test will work for that trigger but the trigger.  this test passes that trigger but i believe you might want to write the trigger to handle bulk updates on leads

 

@isTest
    private class testLeadTrigger
    {
        static testMethod void verifyleadConvert()
        {
        
        Lead l = new Lead();
        l.LastName = 'test';
        l.FirstName = 'test';
        l.Company = 'test';
        l.Status = 'Prospect';
        l.isApproved__c = false;
        insert l;
        
        l.Status = 'Qualified';
        l.isApproved__c = true;
        update l;
        

        List<Lead> retrieveLead = [SELECT id FROM Lead WHERE id =: l.Id AND isConverted = true];
        
        Integer size = retrieveLead.size();
        system.assertEquals(1,size);            

               
        }
    }

 

All Answers

Rahul SharmaRahul Sharma

adambreen,

 

Your trigger is not bulkified, has query and Dml statements inside a for loop, which is not a good practice. Perhaps it will fail while bulk update. Just wanted to let you know this, Which is not issue related to test class failure.

 

The best way to know the failure cause is to put system.debug's in appropriate places in trigger as well as test class.

Check if your trigger is getting fired from test class and also whether its getting inside trigger if loop.

SFAdmin5SFAdmin5

yeah that trigger needs to be re-written to bulkify it. but it will work in the ui  also it needs to be after update not before update.  that won't work with before update i don't think.

 

this test will work for that trigger but the trigger.  this test passes that trigger but i believe you might want to write the trigger to handle bulk updates on leads

 

@isTest
    private class testLeadTrigger
    {
        static testMethod void verifyleadConvert()
        {
        
        Lead l = new Lead();
        l.LastName = 'test';
        l.FirstName = 'test';
        l.Company = 'test';
        l.Status = 'Prospect';
        l.isApproved__c = false;
        insert l;
        
        l.Status = 'Qualified';
        l.isApproved__c = true;
        update l;
        

        List<Lead> retrieveLead = [SELECT id FROM Lead WHERE id =: l.Id AND isConverted = true];
        
        Integer size = retrieveLead.size();
        system.assertEquals(1,size);            

               
        }
    }

 

This was selected as the best answer