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
Lauren BLauren B 

How to write test class for page reference.

Hi alI,
I need to write test class for below apex class and I'm only getting 33% code coverage. I'm not able to cover the page reference in my test class.Please help.
public class TestControllerExtension{
        
         public Test2__c cca {get;set;}
        
        public TestControlleExtension() {
           cca =new Test2__c ();
         
        cca= [select Test2_Request_ID__c,Test2_RecordType__c,Test2_Suffix__c,Test2_DOB__c,Test2_Middle_Name__c,Test2_Flag__c,Test2_Sex__c,Test2_SSN__c,Test2_First_Name__c, Test2_Last_Name__c, Test2_Family_ID__c, Test2ReportDate__c,Test2_BENE_SSN1__c,
                from Test2__c Where Id = :ApexPages.currentPage().getParameters().get('RequestID')];                
        }
        
        public Test2__c  getcca() 
        {
            return cca;
            
        }
        public PageReference NextPage(){
       PageReference p = new PageReference('/apex/TestPageButtonNextPage?RequestID');
              p.setRedirect(false);
              return p;
            }
        
         public PageReference PreviousPage(){
         PageReference p1 = new PageReference('/apex/TestPageViewButton?RequestID');
          p1.setRedirect(false);
             return p1;
           }
        }

 
Best Answer chosen by Lauren B
Suraj Tripathi 47Suraj Tripathi 47
Hi Lauren,

You can take reference from this below code.
@isTest
public class TestControllerExtension_Test {
    @istest static void test(){
        Test2__c tes= new Test2__c();
        tes.name='Sample';
        tes.Test2_First_Name__c='Rahul'';
        tes.Test2_Last_Name__c='Singh';
        tes.Test2ReportDate__c=date.today();
        tes.Test2_Middle_Name__c='Kumar';
        //put the remaining fields if you have to need
        insert tes;
        test.startTest();
        PageReference pageRef = new PageReference('/apex/VFPage');
        pageRef.getParameters().put('RequestID' , tes.id);
        Test.setCurrentPageReference(pageRef);
        TestControllerExtension ce= new TestControllerExtension();
        ce.getcca();
        ce.NextPage();
        ce.PreviousPage();
        test.stopTest();
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Lauren,

You can take reference from this below code.
@isTest
public class TestControllerExtension_Test {
    @istest static void test(){
        Test2__c tes= new Test2__c();
        tes.name='Sample';
        tes.Test2_First_Name__c='Rahul'';
        tes.Test2_Last_Name__c='Singh';
        tes.Test2ReportDate__c=date.today();
        tes.Test2_Middle_Name__c='Kumar';
        //put the remaining fields if you have to need
        insert tes;
        test.startTest();
        PageReference pageRef = new PageReference('/apex/VFPage');
        pageRef.getParameters().put('RequestID' , tes.id);
        Test.setCurrentPageReference(pageRef);
        TestControllerExtension ce= new TestControllerExtension();
        ce.getcca();
        ce.NextPage();
        ce.PreviousPage();
        test.stopTest();
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.
This was selected as the best answer
Lauren BLauren B
Hi Suraj ,
It workerd. I really appriciate your help. Thank you so much.