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
llisallisa 

how to write a test class for page reference?

public PageReference FetchExcelReport() {
        PageReference nextpage = new PageReference('/apex/KDashboardExportToExcel');
        return nextpage;
    }
salesforce mesalesforce me
hi
@isTest
public class CreateAccountTest
{
  static testMethod void testInsertAccount()
  {
  pagereference pa = new pagereference();
   pa.pagereference(‘TestclassAcc1’);
  
  }
}

 
salesforce mesalesforce me
Hi llisa this one also with code coverage check it once....
 
@isTest
public class pagereference 
{
  static testMethod void testpagereference()
  {
  pagereference pa = new pagereference();
  // pa.pagereference (‘pagereference1’);
  
  }
}

 
Himanshu ParasharHimanshu Parashar
@isTest
public class TestPageRef { 

static testMethod void myTest() {

PageReference pg = new PageReference();
pg = controller.FetchExcelReport();
System.assertNotEquals(null, pg); //check for assertion to verify result.
}

}


Above code should work for you.


Thanks,
Himanshu
 
llisallisa
Hi,
    Thanks for your reply.But this code ic not working.
     
Himanshu ParasharHimanshu Parashar
You might be getting an error at line 7 and that is because I am assuming that you have created a instance of your apex class.

If you are new to SFDC it would be great if you can post your class constructor and class name so that I can explain you with correct way.

Thanks,
Himanshu
llisallisa
hello evryone,
                      my code is 

                   public static testMethod void testmycontroller() {
                          PageReference pageRef = Page.KDashboardExportToExcel;
                           Test.setCurrentPage(pageRef);
                            controller.add();//in place of add put function name
                     }
Himanshu ParasharHimanshu Parashar

this is your test class and nobody here can explain you with correct test class without getting your apex class code because everybody will have following questions

1. Is it standard controller or custom controller.
2. if it standard controller what is the sobject type.
3. is it static class ?


So please post your apex class code and then you can ask for explanation.

Makes sense ?