2

I am using the isSandbox() method outlined in the article Determine the organization's edition and environment via APEX code, to determine whether my current org. is a Sandbox.

Sample code from the article:

public Static Boolean isSandbox(){

        String host = URL.getSalesforceBaseUrl().getHost();
        String server = host.substring(0,host.indexOf('.'));

        // It's easiest to check for 'my domain' sandboxes first 
        // even though that will be rare
        if(server.contains('--'))
            return true;

        // tapp0 is a unique "non-cs" server so we check it now
        if(server == 'tapp0')
            return true;

        // If server is 'cs' followed by a number it's a sandbox
        if(server.length()>2){
            if(server.substring(0,2)=='cs'){
                try{
                    Integer.valueOf(server.substring(2,server.length()));
                }
                catch(exception e){
                    //started with cs, but not followed by a number
                    return false;
                }

                //cs followed by a number, that's a hit
                return true;
            }
        }

        // If we made it here it's a production box
        return false;
    }
}

My org. uses custom subdomain. When I attempt to create a unit test class for this method, it returns success at the first branch, i.e.

if(server.contains('--'))
    return true;

and skips the rest of the method. This is leading to only 33.33% code coverage.

I'm looking for pointers on what strategy can I take here to ensure the whole method is tested?

Thanks.

0

1 Answer 1

4

One way to increase coverage, assuming you want to use that method, is to take out all of the if statements. We do this by using the OR operator and returning the result:

public Static Boolean isSandbox(){

        String host = URL.getSalesforceBaseUrl().getHost();
        String server = host.split('\\.',2)[0];
        Boolean isSandbox = false;
        
        // It's easiest to check for 'my domain' sandboxes first 
        // even though that will be rare
        isSandbox |= server.contains('--');
        // tapp0 is a unique "non-cs" server so we check it now
        isSandbox |= server == 'tapp0';
        // If server is 'cs' followed by a number it's a sandbox
        isSandbox |= Pattern.matches('(?i)cs\\d+', server);
        return isSandbox;
    }
}

This reduces the overall complexity of the method without adding any substantial CPU overhead.

However, you may want to just be able to modify this flag in your unit tests, so you might write:

public static Boolean isSandbox;
static {
    calculateSandboxFlag();
}
public static void calculateSandboxFlag(){
    String host = URL.getSalesforceBaseUrl().getHost();
        String server = host.split('\\.',2)[0];
        // It's easiest to check for 'my domain' sandboxes first 
        // even though that will be rare
    isSandbox = server.contains('--') | 
        // tapp0 is a unique "non-cs" server so we check it now
      server == 'tapp0' | 
        // If server is 'cs' followed by a number it's a sandbox
      Pattern.matches('(?i)cs\\d+', server);
}

At this point, you can freely manipulate the flag if you need to, and recalculate it if you need to.

Note that we use the singular | instead of ||, so all logic is executed every time, no "shortcuts" for us. This guarantees 100% coverage if you simply:

System.assert(MyUtils.isSandbox != null);

A single line of code for total coverage (and a lot less code, besides).

Or, finally, just use the IsSandbox flag, that's what it's there for!

public static Boolean isSandbox = [SELECT IsSandbox FROM Organization].IsSandbox;

It is guaranteed there will only be one result for this query. Since it's static, it will never use more than a single query of the 100 limit. This is ideal if you can spare a query.

3
  • Fantastic. These are brilliant examples of tackling the issue from so many different angles. Thank you so much :) Mar 29, 2021 at 5:11
  • Note that the query will return true for a developer edition while the methods above will return false, unless you add a test like server.contains('-dev-ed'). That is not true if the method is called from a visualforce page since in this case the subdomain ends with --c (which contains --). Apr 25, 2021 at 9:06
  • @EmmanuelBRUNO Thanks for the edit. Good point, although the point of the answer was optimization, not necessarily "correctness." PS. The code comes from a Salesforce KB, so my transformation was intended to preserve the original code's intent. This code, I believe, is directed at a customer with Sandboxes, not an ISV with Developer Edition orgs.
    – sfdcfox
    Apr 25, 2021 at 9:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.