+ Start a Discussion
Anju Alexander 5Anju Alexander 5 

Test class for pagerefernce

Hi,
If I am writing a test class for a visualforce page whose controller returns a page refernce. How should I write and what all things should be kept in mind while writing the test class for pagereference?
Best Answer chosen by Anju Alexander 5
praveen murugesanpraveen murugesan
Hi Anju,

You write test class like this for page ref. 

Controller:

public with sharing class ClockPageController {
    public string test{get;set;}

    public ClockPageController(ApexPages.StandardController controller) {
            }
     public PageReference viewCalendar()
    {
        PageReference page =new PageReference('/00U/c');
        page.setRedirect(true);
        return page;
    }
}

TestClass:

@isTest
public with sharing class testClockPageController
{
public static testMethod void test()
{
    Image_Upload__c img = new Image_Upload__c();
    Apexpages.StandardController controller = new Apexpages.StandardController(img);
    ClockPageController clok = new ClockPageController(controller);
    clok.viewCalendar();
    system.assertEquals(img.id,null);

}
}

Mark this as best answer if its helps.

Thanks

Praveen Murugesan

All Answers

Bhawani SharmaBhawani Sharma
Call the method from test class and it should not return null.
System.assert(controller.someMethod() != null);

You can also use PageReference class methods to assert the Page Name, query parameters  etc.
Grazitti TeamGrazitti Team
Hi Alexander,

Please refere the below links:
  1. http://blog.shivanathd.com/2013/11/Best-Practices-Test-Class-in-Salesforce.html
  2. https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
Please mark as best answer if it solves your problem.

Regards,
Grazitti Team,
www.grazitti.com
praveen murugesanpraveen murugesan
Hi Anju,

You write test class like this for page ref. 

Controller:

public with sharing class ClockPageController {
    public string test{get;set;}

    public ClockPageController(ApexPages.StandardController controller) {
            }
     public PageReference viewCalendar()
    {
        PageReference page =new PageReference('/00U/c');
        page.setRedirect(true);
        return page;
    }
}

TestClass:

@isTest
public with sharing class testClockPageController
{
public static testMethod void test()
{
    Image_Upload__c img = new Image_Upload__c();
    Apexpages.StandardController controller = new Apexpages.StandardController(img);
    ClockPageController clok = new ClockPageController(controller);
    clok.viewCalendar();
    system.assertEquals(img.id,null);

}
}

Mark this as best answer if its helps.

Thanks

Praveen Murugesan

This was selected as the best answer
Bhawani SharmaBhawani Sharma
Hi @Praveen: Your test class looks meaningless. What is the use of assert statement like this?
praveen murugesanpraveen murugesan
Hi Bhawani,

I wrote the class. just for an example of pagereference in test class. Agree with you assert stmt should not be like this.

Thanks.
Anju Alexander 5Anju Alexander 5
Hi Grazitti Team,

Thanks for helping.

Thanks and Regards,
Anju Alexander.
Anju Alexander 5Anju Alexander 5
Hi Praveen,

Thanks for giving the example of testing pageReference.

Thanks and Regards,
Anju Alexander.
Anju Alexander 5Anju Alexander 5
Hi Bhavani,

Thanks for helping.

Thanks and Regards,
Anju Alexander.