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
Anu-SFDCAnu-SFDC 

How to cover the test coverage for getURL method

Hi

 

I have a step in the code.

 

public String custom{get;set;}

 

public void GlobalController(){

 custom = ApexPages.currentPage().getURL();

}

 

 

How to write the test coverage for this getURL()  method???

 

Thanks

Anu

sfdcfoxsfdcfox

It appears that this would work:

 

public static void testMethod test() {
Test.setCurrentPage(Page.SomePage);
Test.startTest(); GlobalController();
Test.stopTest(); }

There's no reason to actually assert against system library code, because if it doesn't work, there's not much you can do about it. The only code you need to cover is code that you are writing. For example, if you do something with the string custom, then you would want to validate that it is being used correctly.

 

The only reason to even test the GlobalController function at all is for code coverage. Now, there are system calls that can fail (queries that exceed limits, callouts that fail to connect, etc). In those cases, use the recommended error detection approach (either a try-catch or if-then type detection), and then write a test that simulates failure and success.