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
amit wagaskaramit wagaskar 

how to write test class for below standard controller

my comtroller:
public class myWeb2LeadExtensionIndia 
{
     private final Lead weblead;
    private ID Leadid;
   
    public myWeb2LeadExtensionIndia(ApexPages.StandardController
                                stdController) {
       this.weblead = (Lead)stdController.getRecord();
       this.Leadid    = stdController.getId();
                                     
    }
    
   @testvisible Transient private Attachment myfile;
    public Attachment getmyfile() {
    myfile = new Attachment();
    return myfile;
}

   
     public PageReference saveLead() {
       try {
           
           if(myfile.name==null)
           {
                PageReference pg = apexpages.Currentpage();
                apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.info,'Please Upload An Attachment ');
                apexpages.addmessage(msg);
                return null;
           }
            weblead.LeadSource='Job Application India';
            weblead.Company='added from Job Application Page';
            weblead.Status='open';
            insert(weblead);
            Attachment a = new Attachment(parentid=weblead.id, Name = myfile.name , Body = myfile.Body );
            insert a;
        
           
        
       }
       catch(System.DMLException e) {
           ApexPages.addMessages(e);
           
           return null;
       }
         
         
       PageReference p = Page.ThankYou;
       p.setRedirect(true);
       return p;
     }
    

}
Maharajan CMaharajan C
Hi Amit,

Please try the below test Class you will get the enough coverage:

@isTest
public class myWeb2LeadExtensionIndiaTest {
    
    static testmethod void saveLeadtest()
    {
        Lead ld = new Lead(LastName='Test Lead',Email='Testemail@test.com',Company='Test Company');
        Blob b = Blob.valueOf('Test Data');
        Attachment attachment = new Attachment();
        attachment.Name = 'Test Attachment for Parent';
        attachment.Body = b;  
        ApexPages.StandardController stc = new ApexPages.StandardController(ld);
        myWeb2LeadExtensionIndia weblead = new myWeb2LeadExtensionIndia(stc);
        weblead.myfile = attachment;
        PageReference pageRef = Page.ThankYou;
        Test.setCurrentPage(pageRef);
        weblead.saveLead();
    }
    
    static testmethod void saveLeadtest1()
    {
        Lead ld = new Lead(LastName='Test Lead',Email='Testemail@test.com',Company='Test Company');
        Blob b = Blob.valueOf('Test Data');
        Attachment attachment = new Attachment();
        attachment.Name = 'Test Attachment for Parent';
        attachment.Body = b;  
        ApexPages.StandardController stc = new ApexPages.StandardController(ld);
        myWeb2LeadExtensionIndia weblead = new myWeb2LeadExtensionIndia(stc);
        weblead.myfile = attachment;
        weblead.getmyfile();
        PageReference pageRef = Page.ThankYou;
        Test.setCurrentPage(pageRef);
        weblead.saveLead();
    }
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
Maharajan.C