I am having big problem understanding how namespace works in the RequestURI of Apex Rest Web Services when you deploy them as part of a managed package, and hence, writing a proper Test Class for that, which will run in dev org, packaging org and subscriber org as well.

This is my APEX Rest WS:

global with sharing class BusinessPartnerSyncService {
    global static void updateBusinessPartner() {
        //some processing

My dev org has a namespace bpgdev, and this is what I have written as part of the Test Class

    RestRequest restRequest = new RestRequest(); 
    RestResponse restResponse = new RestResponse();

    restRequest.requestURI = System.Url.getSalesforceBaseUrl().toExternalForm() + '/services/apexrest/bpgdev/SyncSFBusinessPartner';
    //added bpgdev in line above as that is ns of dev org
    restRequest.httpMethod = 'PATCH';
    restRequest.requestBody = Blob.valueOf(gen.getAsString());//gen is a JSONGenerator
    RestContext.request = restRequest;
    RestContext.response = restResponse;


    Blob responseBody = RestContext.response.responseBody;

    System.assert(responseBody != null);

    for (Account account: [SELECT Name FROM Account]) {
        System.assert(account.Name.startsWith('TestAccount') == false);
        System.assert(account.Name.startsWith('partnerName') == true);
    } // all asserts work, web service updates data successfully

I have deployed the whole code to my packaging org where namespace is different (bpgpck), and I totally expected the test class to fail as my dev org namespace is still hardcoded in my test class. I know of the solution here suggested by @DanielBallinger to query for namespace, but that is not my question.

My question is, the whole Test Class succeeds and the data update assertions are all correctly running in the packaging org as well.

So can someone kindly explain, when a test class invokes an Apex Rest web service, does it ignore the namespace in the requestURI altogether?

Sorry if I sound confused, because I am..

1 Answer 1


It's important to give your requestURI a proper-looking value if your code uses it, but strictly speaking, requestURI is a plain String variable, and performs absolutely no validation on its value at any point. You could actually write something like this: restRequest.requestURI = 'Hello, there. How are you? I am fine.'; and your test could still pass, assuming the class you're testing understood how to use such a URI. You don't need the host name, or the namespace, the /service/apexrest/ prefix, or anything else, unless the code you're testing requires it. Using the "wrong" namespace, or even using no namespace at all, is perfectly acceptable if your code still works.

  • Perfect web service is a mashup of Manual Testing and Code UnitTesting. Apr 13, 2018 at 9:17
  • Thanks a lot @sfdcfox .. Actually I got confused in thinking that test class is behaving as a client to my WS (since I am creating the RestRequest and RestResponse objects and not Mocking the response like testing HTTP callouts)... but it is only a method call as I understand now :) Apr 13, 2018 at 11:59

You must log in to answer this question.

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