+ Start a Discussion
Sushma  RamakrishnanSushma Ramakrishnan 

Help to cover Page Reference in Test Class to increase Code Coverage

Hi All,

Below is my Apex Class for which i have got 72% Code coverage and want to make it to 75%.
Apex Class :
public class myWeb2LeadExtension {

    private final Lead weblead;

    public myWeb2LeadExtension(ApexPages.StandardController
                                stdController) {
       weblead = (Lead)stdController.getRecord();
    }

     public PageReference saveLead() {
       try {
       insert(weblead);
       }
       catch(System.DMLException e) {
           ApexPages.addMessages(e);
           return null;
       }
       PageReference p = Page.ThankYou;
       p.setRedirect(true);
       return p;
     }
}

Out of the above lines only the below lines are not getting covered.Please Help...!

PageReference p = Page.ThankYou;
       p.setRedirect(true);
       return p;

Thanks for any help in Advance...!
Best Answer chosen by Sushma Ramakrishnan
Harish RamachandruniHarish Ramachandruni
Hi,


You can add Below Code Copy past in your org . it's working is my org getting 100% code coverage .

 
@istest
public class myWeb2LeadExtensiontest {
    
    
    public static testMethod   void test1(){
        
//for with out exeprtion 
        Lead l = new Lead();
        l.lastname = 'test';
        l.Company = 'tcs';
        l.Status = 'New';
        
        
         ApexPages.StandardController sc = new ApexPages.StandardController(l);

        myWeb2LeadExtension tr = new myWeb2LeadExtension(sc);
        
        tr.saveLead();
        
​//for with exeprtion lead is required comany and status 
        
         Lead l1 = new Lead();
        l1.lastname = 'test';
        
       ApexPages.StandardController s2 = new ApexPages.StandardController(l1);
        myWeb2LeadExtension tr1 = new myWeb2LeadExtension(s2);
        
        tr1.saveLead();
        
        
    }

}


Regards ,
Harish.R
 

All Answers

Harish RamachandruniHarish Ramachandruni
Hi ,


Can you provide your test class . i can help you .


Regards ,
Harish.R
Harish RamachandruniHarish Ramachandruni
Hi,


You can add Below Code Copy past in your org . it's working is my org getting 100% code coverage .

 
@istest
public class myWeb2LeadExtensiontest {
    
    
    public static testMethod   void test1(){
        
//for with out exeprtion 
        Lead l = new Lead();
        l.lastname = 'test';
        l.Company = 'tcs';
        l.Status = 'New';
        
        
         ApexPages.StandardController sc = new ApexPages.StandardController(l);

        myWeb2LeadExtension tr = new myWeb2LeadExtension(sc);
        
        tr.saveLead();
        
​//for with exeprtion lead is required comany and status 
        
         Lead l1 = new Lead();
        l1.lastname = 'test';
        
       ApexPages.StandardController s2 = new ApexPages.StandardController(l1);
        myWeb2LeadExtension tr1 = new myWeb2LeadExtension(s2);
        
        tr1.saveLead();
        
        
    }

}


Regards ,
Harish.R
 
This was selected as the best answer
Sushma  RamakrishnanSushma Ramakrishnan
Hi Harish,

This is my Test Utility Class :
 
@isTest
public with sharing class TestMasterAccountPage {
    public static String CRON_EXP = '0 0 0 15 3 ? 2022';
    static testMethod void constructAccount(){
    test.starttest();
    Account acc = TestingUtils.createAccount('M13', false);
    Account acc1 = TestingUtils.createAccount('N14', true);
    insert acc;
    insert acc1;
    ApexPages.StandardController std = new ApexPages.StandardController(acc);
    MasterAccountExtension controller = new MasterAccountExtension(std);
    controller.getPieWedgeDataList();
    controller.getPieWedgeDataTable();
     PageReference pageRef = new PageReference('/apex/MasterAccountExtension');
     pageRef.getHeaders().put('User-Agent', 'iPad');
     pageRef.setRedirect(true);
     //System.assertEquals(true, MasterAccountExtension.isUsingAndroidPhonegap());
     ApexPages.StandardController std1 = new ApexPages.StandardController(acc1);
     Lead lead = new Lead();
     lead.LastName = 'test';
     lead.Company ='Test';
     insert lead;
     ApexPages.StandardController std2 = new ApexPages.StandardController(lead);
    MasterAccountExtension controller1 = new MasterAccountExtension(std1);
    TmeZoneController tme = new TmeZoneController(std1);
    AutoNotification b5 = new AutoNotification();      
    /*Id BatchprocessId5 = Database.executeBatch(b5); 
   TestrForceAutoNotification sfn =new TestForceAutoNotification();
    SchedulableContext sc  ;
    sfn.execute(sc);*/
    list<AggregateResult> impact1Count = new list<AggregateResult>();
    impact1Count= [select  count(Impact__c) ctr from Company_Incidents__c ];
    list<AggregateResult> impact2Count = new list<AggregateResult>();
     impact2Count= [select  count(Impact__c) ctr from Company_Incidents__c ];
    list<AggregateResult> impact3Count = new list<AggregateResult>();
    impact3Count= [select  count(Impact__c) ctr from Company_Incidents__c ];
    list<AggregateResult> durationCount = new list<AggregateResult>();
    durationCount= [select  SUM(Duration__c) ctr from Company_Incidents__c ];
    MasterAccountExtension.Year yr = new MasterAccountExtension.Year(2015,'Q1');
    // global PieWedgeDataReport(Integer year, String quater, list<AggregateResult> impact1, list<AggregateResult> impact2, list<AggregateResult> impact3, list<AggregateResult> duration) {
    MasterAccountExtension.PieWedgeData pi = new MasterAccountExtension.PieWedgeData('Test', 20.0, 'Q1'); 
    MasterAccountExtension.PieWedgeData pi1 = new MasterAccountExtension.PieWedgeData('Test',20.0);
    MasterAccountExtension.PieWedgeDataReport pReport = new MasterAccountExtension.PieWedgeDataReport(2014,'Q1',impact1Count,impact2Count,impact3Count,durationCount);
    MasterAccountExtension.buildSummaryReport(acc.Id); 
    AccountAjaxUtility.getCompanyLookupId('Account');
    AccountAjaxUtility.VFPageUrl('Account', 'Test', acc.Id);
    AccountAjaxUtility.ObjectPrefix('Account', 'Test', acc.Id);
    AccountAjaxUtility.VFPageUrl('SynchronizeDocumentum', 'IBM', acc.Id);
    AccountAjaxUtility.VFPageUrl('Documents__c', 'Test', acc.Id);
    AccountAjaxUtility.searchStringInaList('Company','Test Roles','Profile');
    EventSaveControllerExtension eve = new EventSaveControllerExtension(std1);
    eve.save();
    ResearchSaveControllerExtension res = new ResearchSaveControllerExtension(std1);
    res.save();
    InnovationTripSaveControllerExtension inv = new InnovationTripSaveControllerExtension(std1);
    inv.save();
    myWeb2LeadExtension web = new myWeb2LeadExtension(std2);
    web.saveLead();
    PageReference p= new PageReference('/apex/ThankYou');
    p.setRedirect(true);
     
     test.stoptest();
     }
     static testMethod void attachmentAccount(){
     test.starttest();
     Account acc = TestingUtils.createAccount('O15', false);
     insert acc;
     Account acc1 = TestingUtils.createAccount('P16', true);
     insert acc1;
     ApexPages.StandardController std1 = new ApexPages.StandardController(acc1);
     Lead lead = new Lead();
     lead.LastName = 'test';
     lead.Company ='IBM';
     insert lead;
     ApexPages.StandardController std = new ApexPages.StandardController(acc);
    MasterAccountExtension controller = new MasterAccountExtension(std);
     ApexPages.StandardController std2 = new ApexPages.StandardController(lead);
    MasterAccountExtension controller1 = new MasterAccountExtension(std1);
    //to save an test attachment
    Attachment attachment = new Attachment();   
        attachment.Name = 'Test Attachment for Parent'; 
        Blob b = Blob.valueOf('Test Data');   
        attachment.Body = b;
        attachment.ContentType='';
        //controller.userAgent=ApexPages.currentPage().getHeaders().get('User-Agent'); 
        //controller1.userAgent=ApexPages.currentPage().getHeaders().get('User-Agent'); 
        controller1.picture = attachment;
        controller1.whichUpload='AccountLogo';
        
        controller1.attachFile();
        controller1.getModel();
        controller1.getContentUrl(b,'Test Attachment for Parent',acc,'');
        controller1.isSF1();
        //controller1.getResults();
        controller.picture = attachment;
        controller.whichUpload='AccountLogo';
        controller.attachFile();
        controller.getModel();
        controller.getContentUrl(b,'Test Attachment for Parent',acc,'');
        controller.isSF1();
        //controller.getResults();
        //to save an test document
        Documents__c doc = new Documents__c();
        doc.Account__c = acc.id;
        doc.Business_Group__c = 'Test';
        doc.Document_Type__c = 'Other';
        doc.SupplierAccountTeamDoc__c = true;
        doc.Corp_Id__c = '123456';
        doc.Doc_Id__c ='0b01e1958097ed';
        doc.Document_URL__c = '/apex/MasterAccountPage';
        
        insert doc;
        delete doc;
        //to execute all the required triggers from test class to get code coverage
        TestingUtils.createTest(acc.Id);
        TestingUtils.createTest(acc.Id);
        TestingUtils.createTest(acc.Id);  
        TestingUtils.createTest(acc.Id);
        //TestingUtils.createTest(acc.Id);
        TestingUtils.createTest(acc.Id);
        TestingUtils.createTest(acc.Id);    
        test.stoptest();
    }
}

 
Harish RamachandruniHarish Ramachandruni
Hi,


You want test class for below class or other class .

 
public class myWeb2LeadExtension {

    private final Lead weblead;

    public myWeb2LeadExtension(ApexPages.StandardController
                                stdController) {
       weblead = (Lead)stdController.getRecord();
    }

     public PageReference saveLead() {
       try {
       insert(weblead);
       }
       catch(System.DMLException e) {
           ApexPages.addMessages(e);
           return null;
       }
       PageReference p = Page.ThankYou;
       p.setRedirect(true);
       return p;
     }
}

for this class you can add below test class it will get 100 % code coverage 



 
@istest
public class myWeb2LeadExtensiontest {
    
    
    public static testMethod   void test1(){
        
        Lead l = new Lead();
        l.lastname = 'test';
        l.Company = 'tcs';
        l.Status = 'New';
        
        
         ApexPages.StandardController sc = new ApexPages.StandardController(l);

        myWeb2LeadExtension tr = new myWeb2LeadExtension(sc);
        
        tr.saveLead();
        
        
         Lead l1 = new Lead();
        l1.lastname = 'test';
        
       ApexPages.StandardController s2 = new ApexPages.StandardController(l1);
        myWeb2LeadExtension tr1 = new myWeb2LeadExtension(s2);
        
        tr1.saveLead();
        
        
    }

}

Regards ,
Harish.R
Sushma  RamakrishnanSushma Ramakrishnan
Thanks so much for all the responses...!
@Harish rao 25 : Your solution worked for me.