Testing the Code That Only Runs in Production

Some code exists to protect Production. A data purge that should only ever run in a sandbox. A job that sends test emails. A reset script for a refreshed environment. The guard at the top of those classes is the most important line in them, and it's also the one line you can't test, because Apex tests run in the org you launch them from. You run tests in a sandbox, so "am I in a sandbox?" is always true during the test.

You can check the org type with a query on Organization.IsSandbox, but that costs a SOQL query every time. System.DomainParser can read it from the org's My Domain URL instead.

public class orgUtil {

    //set ONLY by tests - Apex tests always run in the org they were launched against (a sandbox, in practice)
    //so without this, any code path gated on Production can never be covered
    @TestVisible
    private static Boolean isSandboxOverride; //boolean that denotes whether or not we are currently in a sandbox Org

    //returns true for a sandbox, false for production
    public static Boolean isSandbox {
        get {
            if (isSandboxOverride != null) { //a test has pinned the environment
                return isSandboxOverride; //return the boolean
            }
            System.Domain domain = System.DomainParser.parse(URL.getOrgDomainUrl()); //parse the org's My Domain URL
            return String.isNotBlank(domain.getSandboxName()); //sandbox name is null for a production org
        }
    }
}

The guard in the class you want to protect:

public class sandboxDataPurge {

    public class GuardException extends Exception {}

    public static void run() {
        if (!orgUtil.isSandbox) { //never in production
            throw new GuardException('sandboxDataPurge only runs in a sandbox.');
        }
        //...the actual work
    }
}

And the test that finally reaches it:

@isTest
private class sandboxDataPurgeTest {

    @isTest //defines method for use during testing only
    static void refusesToRunInProduction() {
        orgUtil.isSandboxOverride = false; //pretend this is production
        try {
            sandboxDataPurge.run();
            Assert.fail('The guard should have stopped the run.');
        } catch (sandboxDataPurge.GuardException e) {
            Assert.isTrue(e.getMessage().contains('sandbox'), 'Unexpected message: ' + e.getMessage());
        }
    }
}

Two things worth calling out. The override is private, so only test code can set it, and I leave a comment on it saying nothing in Production assigns to it. Future you (or the next developer) will wonder why it exists, and the comment answers that before anyone "cleans it up". Also, use the same trick anywhere the real value can't happen in a test, like a grace period measured in hours.

Anyway, if a line of code exists to protect Production, it deserves a test that proves it works.

Automated Exchange Rates in Salesforce.com

Reduce Repetitive Tasks, Eliminate Errors & Free Up Your Administrators.

Birthday Reminders for Salesforce.com

It might lead to a sale. Or it might make you feel good.