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
AndreaRozAndreaRoz 

Error: ApexPages.addMessage can only be called from a VF page

Hi to all,
I have got a page with a custom controller, inside the controller I use the "addMessage" to display custom message to the user:
Code:
ApexPages.Message msg;
msg = new ApexPages.Message(ApexPages.Severity.ERROR,'messaggeText');
ApexPages.addMessage(msg);

 
I created my tester class and but when I run test there is following error:
 ApexPages.addMessage can only be called from a VisualForce page, following the piece of code of my testerClass:
Code:
PageReference pageRef = Page.FilialeAssegnazione;
Test.setCurrentPageReference(pageRef);
pageRef.getParameters().put('id',MyCase.Id);
queueController controller;
ApexPages.StandardController stdController;
controller = new queueController(new ApexPages.StandardController(MyCase));

 
How can I test ApexPages.addMessage(msg) instruction in the testerClass?
Best Regards
--
Andrea Rosa
SimbaSimba
Have you found any solution to fix this. I get the same message regarding ApexPages.addMessage method and am not able to write any testMethods for my Apex Page Controller class.

AndreaRozAndreaRoz

Hi Simba,

unfortunately I haven't found a solution for this problem, I am able to pass the test because I created my testMethod so that the line with addMessage instruction is not executed, as you can see this is not a valid solution, please let me know if you fix that problem.

Best
--
Andrea Rosa

 

prashanth reddy 129prashanth reddy 129
Hi guys,
Please refer the link to resolve issue.
http://annappaph.blogspot.com/2017/01/salesforce-finalexception-apexpages.html

Thanks
Prashanth