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
SUMAN KUMARI 70SUMAN KUMARI 70 

Test class for the pagereference method

Hi All, 
Kindly help me with the test class for the following method. 
global PageReference confirmPage(){
        PageReference pageRef;
             if (appointment.id != NULL) {
                 pageRef = new PageReference('/apex/HAZMATconfirmationPage?appointmentId=' + appointment.id + '&cId=' + HAZMATCase.id);
        
             } else {
                 pageRef = new PageReference('/apex/HAZMATconfirmationPage?caseId=' + HAZMATCase.id);
        
             }
        return pageRef;
        }

 
AnudeepAnudeep (Salesforce Developers) 
Below is an example of how page reference method is tested in general

Controller class
 
public with sharing class opportunityController{
    public Opportunity opp {get;set;}
    public opportunityController(){
        opp = new Opportunity ();
    }

    public pageReference save(){
        insert opp;
        pagereference page =new ApexPages.StandardController(opp).view();//pageReference ('/apex/OpportunityPage');
        page.setRedirect(true);
        return page;
    }
}

Test Class
 
@isTest

public class testConfirmPage{
    public static testMethod void test () {

       // replace opportunityController with your controller

        opportunityController oppC = new opportunityController ();

       // replace Opportunity object with the object referenced primarily in the controller
        Opportunity opp = new Opportunity ();
        pageReference pager = page.HAZMATconfirmationPage; (use your page name here)
        Test.setCurrentPage(pager);
        opp.Name  = 'abc';
        opp.Stagename = 'Prospecting';
        opp.Closedate = system.today();
        insert opp;
        oppC.confirmPage();
        apexPages.Currentpage().getParameters().put('Id',opp.id);
   }
}

NOTE: The code provided is an example. You'll need to review and make modifications for your organization.

Let me know if this helps. If it does, please mark this answer as Best. It may help others in the community. Thank You!