5

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...

3
  • 4
    Are you able to post a snippet of your test code to help us understand a bit better what your doing here? Thanks! :) Commented Jun 12, 2013 at 20:34
  • Specifically, if you could post the Assert statement, that would be helpful.
    – Matt K
    Commented Jun 12, 2013 at 20:58
  • Looking at the code sample below, 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? Did it ever work btw? Commented Sep 12, 2013 at 9:03

3 Answers 3

3

Looking at the code sample you have provided, it does look like the UserInfo.getSessionId() method is context sensitive, to the point where if it's directly in a test method vs. an Apex class it behaves differently. I've definitely found that not all Session IDs are equal in the past--Salesforce will generate different variants for the same physical session.

I suspect this is what you're seeing. Clearly you want to follow best practice and test your code. This maybe one where you simply have to settle for coverage alone.

2

In general, you should avoid testing "system" methods. You can logically assume that these functions "always" work, because if they didn't, then there'd be far larger problems than just your code. In the event of complex logic, it would suffice to ensure that your logic is working via the presence or lack of a value (e.g. null when you expect it to be, or not-null when you expect it to be).

1
  • The sessionId was part of a web service which required sessionId to be present, thus the test method included verification of the sessionId. But the main concern is that these values are different between sandbox (literal value "ApexTestSession") and production (actual value differing if called more than once in the same test method). I expected consistency. Commented Oct 9, 2013 at 19:54
0

The question has been modified to include the answer. In summary, the test method checks all URL params excluding sessionId and confirmationToken.

1
  • 1
    Thanks for the update on your question. Can you please place this additional detail in the original question rather than posting it as an answer, thanks. Commented Sep 12, 2013 at 8:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .