3

I am writing a test class for one of my controllers. And in the test class i am setting the current page as given below

pagereference pageref1 = new pagereference('/apex/E2E_Screen_2');
        pageref1.getParameters().put('id', Abolish.id);
        Test.setCurrentPage(pageRef1);

Now in the controller I am using the Site.getPathPrefix() to get the value from the URL, but the value that is returned from site.getPathPrefix() is null. I tried to print the URL of the current page using

string x = ApexPages.currentPage();
srting y = ApexPages.currentPage().getURL();

The value of the URL is displayed correctly. Can anyone please tell me why the site.getPathPrefix() value is null here?

Thanks

1 Answer 1

5

Methods of the Site class (including getPathPrefix) depend on the specific site the code is running in. That site definition is separate from the Visualforce and Apex you create and there is no built-in mechanism in in the testing framework to fake running in a site.

So if you are testing some logic that requires there to be a prefix, you can modify the code you are writing the test for to get the prefix from a method:

private String getPathPrefix() {
    return Test.isRunningTest() ? 'fakePathPrefix' : Site.getPathPrefix();
}

and your test can rely on the fake value.

(This sort of test conditional logic is a last resort.)

1
  • I'll see if I can add the mentioned logic. Thanks! Commented Jun 19, 2015 at 13:11

You must log in to answer this question.

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