1

For example, with the following code which is intended to update version numbers on the User object...

@RestResource(urlMapping='/Versions/*')
global with sharing class versionsController {

    @HttpPatch
    global static void patchVersions(String iOSVersion, String iAppVersion) {
        // ... method body to update User record ...
    }

}

I see that its possible from the test method, to just pass the parameters in without using a RestContext, and get coverage that way, but is this best practice? And if RestContext is used, how should it be used in this case?

EDIT: I believe my question is different from those others posted on Salesforce SE since it specifically asks about whether or not there is a need or benefit to using RestContext when testing parameterized POST or PATCH methods. The other questions seem to be about testing Apex REST methods in a more general sense.

  • 1
    Yes that is best practice. Even if you did a callout during a test, then endpoint would never be hit as you would be mocking the response. So you have no choice but to call the method directly in the test method – Eric Dec 26 '16 at 21:11
  • 1
    Possible duplicate of Writing Test Classes for Apex RestService – Eric Dec 26 '16 at 21:12
  • Thank you Eric, but I also see accepted answers like this (link attached to this comment), which leaves me a little confused (they seem to contradict your comment above - I have no idea who is right) and makes me want to keep this question open in order to get a variety of opinions on this testing case which must surely be quite a common one salesforce.stackexchange.com/questions/23597/… – Brad Thomas Dec 26 '16 at 21:19
  • You are confused ;) the linked answer is not making a call out. They are instantiating the method directly. You still have to set the test context. Review duplicate answer I linked to – Eric Dec 26 '16 at 21:23
  • Dang phone - supposed to have sed "RestContext" if needed not test context – Eric Dec 26 '16 at 21:59
3

You can indeed call the methods directly. Many Apex REST methods make use of the RestContext, and if they do so, you need to set those context variables in your test to avoid a NullPointerException. Unlike ApexPages.currentPage, RestContext.request and RestContext.response are null by default in your tests. If your code doesn't use them, however, don't bother to set them.

A common example:

REST

@HttpPost
global static void doStuff()
{
    String external = RestContext.request.requestUri.substringAfterLast ('/');
}

Test

static testMethod void testRest()
{
    RestContext.request = new RestRequest();
    RestContext.request.requestUri = '/Versions/123';
    MyRestClass.doStuff();
}
  • Ok thank you, so, if doStuff had parameters as part of its signature, there would be no way to test the system's REST deserialization of those parameters from a request? We just pass the parameters in directly? – Brad Thomas Dec 26 '16 at 21:41
  • 2
    @BradThomas You don't need to unit test the system (e.g. deserialization), because salesforce.com assures us that it works, primarily by way of internal regression testing. As you learn more about Salesforce, you'll learn certain truths, such as the fact that a query will never return a null value instead of a record, list, or number. – sfdcfox Dec 26 '16 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.