Test a method returning a PageReference in Salesforce

yatish mehta picture yatish mehta · Feb 3, 2012 · Viewed 8.8k times · Source

I have a method in the controller

    public PageReference add() {
              insert technology;
              return null;
            }

technology is a custom object .It has its own getters and setters.How do I test this method

Answer

Bhushan Lodha picture Bhushan Lodha · Feb 10, 2012
 public static testMethod void testMyController() {


   PageReference pageRef = Page.yourPageName;

   Test.setCurrentPage(pageRef);

   MyController controller = new MyController();
   controller.add();

}