Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in Salesforce by (11.9k points)

I am developing a Rest Web service in platform. I wrote it and it is working properly and giving accurate responses when I call it. The problem is how to test it


    global class Feedpost9



      global static User updateCase() {

         RestRequest req=RestContext.request;

         User user=[SELECT id from User where email=:req.headers.get('email') LIMIT 1];

         return user;          



Now my task is to test this REST web service method updateCase. For testing a POST request method I have to set the parameters in method call but in case of PUT method - how do I set RestContext.request in the test class?

1 Answer

0 votes
by (32.1k points)
edited by

Issues which are needed to be contented with unit tests are:

  • The lack of data in a test environment
  • The need for a valid RestRequest

The basic technique needed to set up the request object your method wants is:

global class Feedpost9 {

    // your methods here...

    static testMethod void testRest() {

        // set up the request object

        System.RestContext.request = new RestRequest();

        RestContext.request.requestURI = '/feedpost/whatever';

        RestContext.request.addHeader('email', '[email protected]');

        // Invoke the method directly




Want to become a Salesforce expert, join Salesforce Online Training now! 

Browse Categories