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
Manish Verma 4Manish Verma 4 

How to write a test class?

Hi all,
i wrote an apex class to create a custom lead page ,I need to deploy my code pls help me in writing test class for this.

public with sharing class LeadExtension {
private String RecordTypeId, RecordTypeName;
    Public String LeadRecId{get;set;}
    Private Boolean reDirect;
    Private Lead objLead;
    Private ApexPages.StandardController controller;
    // Parameterize constructor define below 
    public LeadExtension(ApexPages.StandardController controller) {
    
        //this.RecordTypeId =String.valueOf((Lead)controller.getRecord().RecordTypeId);
        objLead= (Lead)controller.getRecord();
        RecordTypeId=objLead.recordTypeId;
        LeadRecId=ApexPages.currentPage().getParameters().get('id');
       
        RecordTypeName = '';
        for(RecordType rt:[select id,name from recordtype where id =:recordtypeid])
        RecordTypeName = rt.name;
        system.debug('*********************'+RecordTypeName );
        if(RecordTypeName==' Sales'){
            reDirect=false;
        }else{
           reDirect=true;
        }
    }
    // getter method call by lead edit page
    public boolean getRedirect()
    {   if(RecordTypeName=='Sales'){
           // reDirect=false;
            return false;
        }       
       return true;
    }
public string getRecordTypeId()
{
return RecordTypeId;
}
/*public string getLeadId()
{
return LeadId;
}*/

 Public Pagereference saveMethod(){
    Account acc=[select name from account where id =:objLead.AccountName__c limit 1];
    Contact con=[select name from contact where id =:objLead.Contact_1__c limit 1];
    objLead.lastName=acc.name;
    objLead.company=con.name;
    if(objLead.id==null){
        objLead.Sales_Rep__c=UserInfo.getUserId();
        objLead.status='Qualified';
    }
    ApexPages.StandardController sc = new ApexPages.StandardController(objLead);
    PageReference pr = sc.save();
    //pageReference pv = GstdController.view(); return pv; } return null;
    
  return pr;
  }
  Public Pagereference reDirect(){
     Pagereference pr;
      if(reDirect && LeadRecId==null){
        pr=new Pagereference('/00Q/e?RecordType='+recordtypeid+'&nooverride=1');
        return pr;
      }
     return null;
  }
}


Thanks & regards
Manish Verma