[SalesForce] Failure of a Test Method due to UserInfo.getSessionId value

Test method which asserts expected and actual URL fails on PROD while there are no issues on a sandbox. PROD has the actual sessionId in the URL, while sessionId value at a sandbox is: SessionId=00De0000000abcZ%21ApexTestSession

Is it possible that the actual sessionId in a test method is causing problem at PROD?

Solution

We have changed test method to determine which parameter actually causes the issue and found out sessionID comparison results in a failure indeed. This issue is only reproducible on PROD while it works fine on a sandbox due to difference in how sessionID is generated in a test method based on the env.

Salesforce support indicated this is as expected and did not provide technical details for the issue.

Failure Message on PROD:
"System.AssertException: Assertion Failed: Parameter: SessionId mismatch:
Expected: 00D30000000abcZ!ARcAQEGRFJyOj………,
Actual: 00D30000000abcZ!ARcAQJSCH2mOlknHRvf…….

Below is the sample code to reproduce assert failure on PROD.

public class MySession{
   public static String getSessionId() {
       return UserInfo.getSessionId();
   }
}


@isTest
public class MySessionTest{
   @isTest static void getSessionIdShouldBeSameAsUserInfoSessionId() {
       String expected = UserInfo.getSessionId();
       String actual = MySession.getSessionId();     
       System.debug('Expected: ' + expected + ', Actual: ' + actual);
       System.assertEquals(expected, actual);
   }
}

**FYI: there was one more issue generated on PROD only in a test method comparing Salesforce standard URL. When testing standardController.delete(), the URL in PROD has confirmation token which is not present on any sandbox. As a resolution, we have changed test method to compare only parameters we are interested in and not complete URL.

Sample URL generated on PROD in a test method:
/setup/own/deleteredirect.jsp?CONFIRMATIONTOKEN=……&delID=a2ha0000000abcX&nooverride=1&retURL=%2Fa2h%2Fo…

Best Answer

Looking at the code sample you have provided, it does look like the UserInfo.getSessionId() method is context sensitive, to the point where if its directly in a test method vs a Apex class it behaves differently. I've definitely found that not all Session ID's are equal in the past, Salesforce will generate different variants for the same physical session. I suspect this is what your seeing. Clearly your wanting to follow best practice and test your code, this maybe one where you simply have to settle for coverage alone. Interested to see what Salesforce have to say....

Related Topic