0

I'm trying to refactor the following code.I'm trying to figure out how to refactor this code OR if anyone have a good way to do code coverage for this.

My issue is that, in dev, the code coverage is only the if-else statement for '%DEV%' and the rest are not covered in dev because its only getting the URL base url for the respective environment. In QA, the code coverage is only the if-else statement for '%QA%' and the rest are not covered. So on and so forth...

The custom labels contain the following values.


System.Label.DevSB-->dev
System.Label.QASB-->qa
System.Label.StageSB-->stage
System.Label.IntSB-->stage

Our Salesforce Sandbox Instances and URLs are as follow.


mycompany--dev20.my.salesforce.com --> dev
mycompany--qa20.my.salesforce.com --> qa
mycompany--stage.my.salesforce.com --> stage
mycompany--int.my.salesforce.com --> stage environment but for integration purposes
mycompany.my.salesforce.com --> prod

Essentially we have this method and if this method is executed in our dev environment (mycompany--dev20.my.salesforce.com) it should return '%Dev%', and if its executed in qa environment (mycompany--qa20.my.salesforce.com), then it should return '%QA%' so on and so forth....

public static String getSandboxName(){
    
    if(URL.getSalesforceBaseUrl().getHost().containsIgnoreCase(System.Label.DevSB)){
        return '%Dev%';
    } else if(URL.getSalesforceBaseUrl().getHost().containsIgnoreCase(System.Label.QASB)){
        return '%QA%';
    } else if(URL.getSalesforceBaseUrl().getHost().containsIgnoreCase(System.Label.StageSB) || URL.getSalesforceBaseUrl().getHost().containsIgnoreCase(System.Label.IntSB)){
        return '%Stage%';
    } else {
        return '%PROD%';
    }
}

This is my test classes but unfortunately, it is not working.

static testMethod void testGetSandboxNameDEV(){

    String actualSandboxName;
    PageReference pageRef;

    pageRef = new PageReference('https://mycompany--dev20.my.salesforce.com');
    Test.setCurrentPage(pageRef);
    actualSandboxName = MyClass.getSandboxName();
    System.Debug('baseUrl-->' + URL.getSalesforceBaseUrl().getHost());
    System.assertEquals('%Dev%', actualSandboxName);
    
}

static testMethod void testGetSandboxNameQA(){

    String actualSandboxName;
    PageReference pageRef;

    pageRef = new PageReference('https://mycompany--qa20.my.salesforce.com');
    pageRef.setRedirect(true);
    Test.setCurrentPageReference(pageRef);
    actualSandboxName = MyClass.getSandboxName();
    System.Debug('baseUrl-->' + URL.getSalesforceBaseUrl().getHost());
    System.assertEquals('%QA%', actualSandboxName);
    
}   


static testMethod void testGetSandboxNameSTAGE(){

        String actualSandboxName;
        PageReference pageRef;

        pageRef = new PageReference('https://mycompany--stage.my.salesforce.com');
        pageRef.setRedirect(true);
        Test.setCurrentPageReference(pageRef);
        actualSandboxName = MyClass.getSandboxName();
        System.Debug('baseUrl-->' + URL.getSalesforceBaseUrl().getHost());
        System.assertEquals('%Stage', actualSandboxName);
        
}  

1 Answer 1

2

You can't override the return value of URL.getSalesforceBaseUrl() in a test class.

There's two possible approaches here. One is to factor out the code that calls that method into a separate code unit so that you can utilize dependency injection to control its return value. But that seems like overkill here. There's a much simpler option: refactor your code so that it doesn't use conditional logic you can't cover:

public static String getSandboxName() {
    Map<String, String> urlMap = new Map<String, String>{
        System.Label.DevSB => '%Dev%',
        System.Label.QASB => '%QA%',
        System.Label.StageSB => '%Stage%',
        System.Label.IntSB => '%Stage%',
        'salesforce' => '%PROD%'
    };

    for (String k : urlMap.keys()) {
        if (URL.getSalesforceBaseUrl().getHost().containsIgnoreCase(k)) {
            return urlMap.get(k);
        }
    }
}

(Disclaimer, typed here and not tested).

That should allow you to get full coverage regardless of which environment the test runs in because every branch is executed.

1
  • This code snippet helped! Thank you
    – jojopogi
    Commented Nov 8, 2020 at 14:34

You must log in to answer this question.

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