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
sroberts_MEsroberts_ME 

testing coverage for a controller extension

static testMethod void AssetAtRiskTest(){

        Account acc = new Account(name = 'foo');

      insert acc;

 

        ApexPages.StandardController sc = new ApexPages.StandardController(acc);

        PageReference pageRef = sc.view();

               

        AssetAtRisk a = new AssetAtRisk(sc);                

        test.startTest();       

        test.setCurrentPage(pageRef);       

        pageRef = a.autoRun();                       

        test.stopTest();

}

 

so this is my test code minus the assertions of course. The issue is that when the autorun function is running the following line always produces null.

 

String theId = ApexPages.currentPage().getParameters().get('id');

 

I would expect this line to set theId to the Id of the new account I created in the test method, however this is not the case. Am I missing something simple here?

Best Answer chosen by Admin (Salesforce Developers) 
Edwin VijayEdwin Vijay

you can set the parameters for the page using this line of code

 

ApexPages.currentPage().getParameters().put('id',acc.Id);

 So, the test class will be

 

static testMethod void AssetAtRiskTest(){

        Account acc = new Account(name = 'foo');

      insert acc;

 

        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
ApexPages.currentPage().getParameters().put('id',acc.Id);
        PageReference pageRef = sc.view();

               

        AssetAtRisk a = new AssetAtRisk(sc);                

        test.startTest();       

        test.setCurrentPage(pageRef);       

        pageRef = a.autoRun();                       

        test.stopTest();

}

 Let me know if it worked..

All Answers

Edwin VijayEdwin Vijay

you can set the parameters for the page using this line of code

 

ApexPages.currentPage().getParameters().put('id',acc.Id);

 So, the test class will be

 

static testMethod void AssetAtRiskTest(){

        Account acc = new Account(name = 'foo');

      insert acc;

 

        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
ApexPages.currentPage().getParameters().put('id',acc.Id);
        PageReference pageRef = sc.view();

               

        AssetAtRisk a = new AssetAtRisk(sc);                

        test.startTest();       

        test.setCurrentPage(pageRef);       

        pageRef = a.autoRun();                       

        test.stopTest();

}

 Let me know if it worked..

This was selected as the best answer
sroberts_MEsroberts_ME

Thanks Edwin,

 

That line of code worked perfectly, you just have to make you change the current page before you set the Id.