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
SudhanwaSudhanwa 

Dynamically fetching the URL makes the test coverage go down

Hi,

 

In my controller, I had a situation where I had to give a link to another page. I had it hard coded earlier to the following-

 

body { margin: 0 0 0 0; padding:0 0 0 0 } td,div { font-family:Tahoma;font-size:8pt;vertical-align:top } body { margin: 0 0 0 0; padding:0 0 0 0 } .transcript { background-color:#d2d2d2; } .messageBlock { margin-left:4px; margin-bottom:3px } .message { margin-left:100px; word-wrap:break-word; white-space:-moz-pre-wrap; _white-space:pre; } .messageCont { margin-left:100px; word-wrap:break-word; white-space:-moz-pre-wrap; _white-space:pre;} .other { color:#39577a;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .myself { color:#da8103;font-style:normal;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont { font-size:8px;text-align:right; color:#39577a;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .myselfCont { font-size:8px;text-align:right; color:#da8103;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .system { margin-left:4px; word-wrap:break-word;color:#da8103;font-style:normal;font-weight:normal; white-space:-moz-pre-wrap; _white-space:pre; } .showTimestamp { margin-right:3px; float:right; color:#999999;font-style:normal;font-weight:normal; } .other1 { color:#ac2000;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont1 { font-size:8px;text-align:right; color:#ac2000;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other2 { color:#3c9fa8;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont2 { font-size:8px;text-align:right; color:#3c9fa8;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other3 { color:#e25614;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont3 { font-size:8px;text-align:right; color:#e25614;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other4 { color:#0b6ac8;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont4 { font-size:8px;text-align:right; color:#0b6ac8;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other5 { color:#b23290;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont5 { font-size:8px;text-align:right; color:#b23290;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other6 { color:#02e7c7;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont6 { font-size:8px;text-align:right; color:#02e7c7;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other7 { color:#5b3284;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont7 { font-size:8px;text-align:right; color:#5b3284;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .tsDisplay { display:block }

.  

serverurl = 'https://na3.salesforce.com/apex/MyPage'; 

 

The code coverage then was well above 75%. But as its not a good practice I replaced the above with the following lines wherever it occured- 

 

serverurl = serverurl.substring(serverURL.indexOf('https://'),serverURL.indexOf('salesforce.com'));
            serverurl = serverurl+'salesforce.com/apex/MyPage';

 

This made the code coverage nose dive and I am unable to deploy it to production. 

 

Where am I being wrong here; or is there any specific way to do it in a test method?

 

Thanks,

Sudhanwa.

sfdcfoxsfdcfox

Unless you're using an Http/HttpRequest/HttpResponse sequence in Apex Code, don't use a serverurl; instead, just use a relative link, like so:

 

url="/apex/MyPage";

 

Most of the time, that will work correctly, as browsers (and the PageReference object) will figure out that you mean the current server.

 

It's odd that using such code would cause a nose-dive in coverage though. Must be some conditional branches you added... you didn't list them here, but that's probably your cause.