+ Start a Discussion
RamKannanRamKannan 

Test class for Apex class .

 

Hi, 
I have created a extension class for Lead object. To insert the record from the site.
I wouldnot be able to get the code coverage upto 75%
Can any one please help me out in covering all the lines of code.
 
Apex Class : 
public with sharing  class leadExtension {

    private final ApexPages.StandardController controller;
    
       public leadExtension(ApexPages.StandardController stdController) {
      
        this.controller = stdController;
        
    }

 
    public PageReference Save()
    {
      
      lead l = (lead)controller.getRecord();
      insert l;
    
     Pagereference thanksPage2= Page.thanksPage2;
    hpathanksPage.setRedirect(True);
    return hpathanksPage;
    
    }
        
 public PageReference Submit()
    {
      
      lead l = (lead)controller.getRecord();
      insert l;
     Pagereference thanksPage= Page.thanksPage1;
   eppthanksPage.setRedirect(True);
     return eppthanksPage;
   
    }
}

================================================================

Test class :

@isTest
private class TestleadExtension  {
 


  static testMethod void Leadextension() {
 
    Lead l = new Lead();
  Leadextension controller = new Leadextension(new ApexPages.StandardController(l));
  
    l.LastName='testname';
    l.Company='testcompany';
    l.Internal_Contact_Title__c='testtitle';
    l.Internal_Contact_Phone_Number__c='123456';
    l.Internal_Contact_Name__c='testcontact';
    l.Internal_Contact_email_Address__c='testb@company.com';
    l.Phone='45678';
    l.Email='test@company.com';
    l.HR_Contact_Name__c='test HRContact';
    l.HR_Contact_Title__c='test HRContact title';
    l.HR_Contact_Phone_Number__c='6789';
    l.HR_Contact_Email_Address__c= 'test@company.com';         
    insert l;
   
    }
  static testMethod void save() {
 
    pageReference Pageref1 =Page.thankspage2;
      Test.setCurrentPage(Pageref1);
      Pageref1.setRedirect(true);
     
   
        }
       
    static testMethod void Submit() {
   
       
     PageReference pageRef2 = Page.hankspage1;
                  Test.setCurrentPageReference(pageRef2);
     Pageref2.setRedirect(true);
   
    }
   
}

Please help me out on this issue .

 

Thanks

Ramesh

jeffdonthemic2jeffdonthemic2

You need to call your methods in the controller and assert that certain values/behaviors behaved correctly. Take a look at the following blog post and see if it helps...

 

Testing Salesforce.com Controller Extensions

 

Jeff Douglas

Appirio, Inc.

http://blog.jeffdouglas.com

 

Author: The Salesforce Handbook