Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test a method returning a PageReference in Salesforce

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

like image 533
yatish mehta Avatar asked Jul 12 '26 05:07

yatish mehta


2 Answers

 public static testMethod void testMyController() {


   PageReference pageRef = Page.yourPageName;

   Test.setCurrentPage(pageRef);

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

}
like image 167
Bhushan Lodha Avatar answered Jul 13 '26 19:07

Bhushan Lodha


In test class You had to initialize Your controler and call this method:

static testMethod void test(){
    YourController contr = new YourController();
    contr.add();
}

Hope that helps.

like image 31
Łukasz Skrodzki Avatar answered Jul 13 '26 20:07

Łukasz Skrodzki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!