function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
adrisseladrissel 

Setting baseUrl for Test Class

Hey all,

I am writing a test class and need to know how to set the baseUrl based on the following method in my controller:
public String getBaseUrl(){
        
        vfUrl = Url.getSalesforceBaseUrl().toExternalForm();
        Integer iIndex = vfUrl.indexOf('.visual');
        instance = vfUrl.substring((iIndex-4),(iIndex));
        baseUrl = 'https://'+instance+'.salesforce.com';
        
        return baseUrl;
    }
This method is built to convert the VF page url it begins with into the simple instance URL.  However, when I run the following test class I get the error:

"System.StringException: Starting position out of bounds: -5"

Test Class:
@isTest
private class Five9ScreenPopTest{

    @isTest static void testFive9ScreenPop(){
    
        CreateUser cu = new CreateUser();        
        User u = cu.createUser();
        ApexPages.StandardController cont;
        
        System.RunAs(u){
        
            String ani = '8005551212';
            Test.setCurrentPage(pageRef);
            
            Five9ScreenPop f9sp = new Five9ScreenPop(cont);
            PageReference numCases = f9sp.NumCases();
        }
    }
}
NumCases() is a method within the Five9ScreenPop class.

How do I set the baseUrl for my testing here?  Do I need to pass a starting URL to the controller?  How do I do that?

Thanks!

 
pconpcon
Where are you getting pageRef on line 13 of your test from?  I wonder if that pageRef is set to what you expect it to be.  If it is null it may cause that Url method to break.
adrisseladrissel
Oops...sorry, left that out for some reason. Here is the full thing:
@isTest
private class Five9ScreenPopTest{

    @isTest static void testFive9ScreenPop(){
    
        CreateUser cu = new CreateUser();        
        User u = cu.createUser();
        PageReference pageRef = new PageReference('https://c.cs24.visual.force.com');
        ApexPages.StandardController cont;
        
        System.RunAs(u){
        
            String ani = '8005551212';
            Test.setCurrentPage(pageRef);
            
            Five9ScreenPop f9sp = new Five9ScreenPop(cont);
            PageReference numCases = f9sp.NumCases();
        }
    }
}

 
pconpcon
In doing my tests, I don't think you can reproduce what you have there inside of a test.  Everything I do always returns the non-visualforce url.  What I would sugges is that you do the following:
 
public String getBaseUrl() {       
    String vfUrl = (Test.isRunningTest) ?
        'https://c.cs24.visual.force.com' :
        Url.getSalesforceBaseUrl().toExternalForm();
    Integer iIndex = vfUrl.indexOf('.visual');
    String instance = vfUrl.substring((iIndex-4),(iIndex));
    String baseUrl = 'https://' + instance + '.salesforce.com';

    return baseUrl;
}

That'll set your vfUrl differently if you are in test mode.
adrisseladrissel
Now getting this:

"Compile Error: Variable does not exist: Test.isRunningTest at line 200 column 18"

What variable do I need to create?
pconpcon
sorry
 
public String getBaseUrl() {       
    String vfUrl = (Test.isRunningTest()) ?
        'https://c.cs24.visual.force.com' :
        Url.getSalesforceBaseUrl().toExternalForm();
    Integer iIndex = vfUrl.indexOf('.visual');
    String instance = vfUrl.substring((iIndex-4),(iIndex));
    String baseUrl = 'https://' + instance + '.salesforce.com';

    return baseUrl;
}