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
sona gaikwadsona gaikwad 

How to write a test class for an apex trigger on lead

trigger
trigger MyTrigger on Lead (before insert,before update) {
    
leadhandlerclass handler =new leadhandlerclass();
    
    if(Trigger.isInsert && Trigger.isbefore)
    {
        handler.UpdateLeadStatus(Trigger.new); 
    }

    if(Trigger.isupdate && Trigger.isbefore)
    {
        handler.UpdateLeadStatus(Trigger.new); 
    }
    
}

Testlead
@istest 
private class Testlead {
    
static testmethod void addressOverride(){

      Lead s = new Lead();  
      s.stages__c = 'Booked';
      leadhandlerclass handler =new leadhandlerclass();
      Lead  acc = new  Lead (stages__c = 'Booked');
      insert acc;
      system.debug('----->>> stages__c: ' + acc.stages__c);
    
      Test.startTest(); 
      system.debug('----->>> status__c: ' + acc.status__c);
      Test.stopTest();
  
    }

}
Best Answer chosen by sona gaikwad
Ajay K DubediAjay K Dubedi
Hi Sona,

Below Code can fulfill your requirements, Hope this will work for you.
@istest 
private class Testlead {
    
static testmethod void addressOverride(){

      Lead s = new Lead();  
      s.stages__c = 'Booked';
      insert s;
      
      s.stages__c = 'Booked new';
      update s;
    
      Test.startTest(); 
      system.debug('----->>> status__c: ' + acc.status__c);
      Test.stopTest();
  
    }
}

Please mark this as best answer if this solves your problem.

Thank you,
Ajay Dubedi    

 

All Answers

Raj VakatiRaj Vakati
try this
 
@istest 
private class Testlead {
    
static testmethod void addressOverride(){

 Test.startTest(); 
     
	 
	 // create a Lead
Lead lead1=new Lead(LastName='Doe',FirstName='John',Company='Test',Status='Open' ,stages__c = 'Booked');
insert lead1;     
    
    
lead1.LastName ='Test update' ; 

update lead1 ; 
	
      Test.stopTest();
  
    }

}

 
Ajay K DubediAjay K Dubedi
Hi Sona,

Below Code can fulfill your requirements, Hope this will work for you.
@istest 
private class Testlead {
    
static testmethod void addressOverride(){

      Lead s = new Lead();  
      s.stages__c = 'Booked';
      insert s;
      
      s.stages__c = 'Booked new';
      update s;
    
      Test.startTest(); 
      system.debug('----->>> status__c: ' + acc.status__c);
      Test.stopTest();
  
    }
}

Please mark this as best answer if this solves your problem.

Thank you,
Ajay Dubedi    

 
This was selected as the best answer