I am developing a Apex REST class for an external system. I have done with the development and everything is working good.

Now I am creating test classes for this Rest class.

Problem I want to set params in RestContext.request.params but it is not setting parameters.

Below is my Rest class

global class Rest_Services_Owner_Portal {

    Private static String ACTION_PARAM =  'action';
    Private static String TEST_SERVICE =  'testService';
    Private static String GET_BANK_DETAILD =  'getBankDetails';
    Private static String EDIT_BANK_DETAILD =  'editBankDetails';
    Private static String CREATE_BANK_DETAILD =  'createBankDetails';
    public static Object result;
    public static String jsonString{get{
        return JSON.serialize(result);
    global static void  handleRequest() {
        RestResponse response = RestContext.response;
            RestRequest req = RestContext.request;
            Map<String,String> headersMap = req.headers;
                throw New CustomException('action parameter is required!');
                throw New CustomException('CRS authorization header missing!');
            String action = req.params.get('action');
                result = Rest_Services_Owner_Portal_Handler.testService(req.requestBody.toString());
                throw New CustomException('Invalid action!');
        }catch(CustomException cx){
            response.statusCode = 403;
            result = New ResponseClass('error',null,cx.getMessage(),cx.getMessage());
        }catch(Exception ex){
            result = New ResponseClass('error',null,ex.getMessage(),ex.getMessage());
        response.responseBody = Blob.valueOf(jsonString);

And following is my ServiceHandler class

public  class Rest_Services_Owner_Portal_Handler {
    public Rest_Services_Owner_Portal_Handler() {


    public static Object testService(String data){

        return New ResponseClass('success',JSON.deserializeUntyped(data),'Services are working!','Services are working!');

And following is my Response Class

public with sharing class ResponseClass {
    public String status{get;set;}
    public Object result{get;set;}
    public String message{get;set;}
    public String debugMessage{get;set;}
    public ResponseClass(){

    public ResponseClass(String status,Object result,String message,String debugMessage) {
        this.status = status;
        this.result = result;
        this.message = message;
        this.debugMessage = debugMessage;

Below is my Test class

private class Test_Rest_Services_Owner_Portal {

    @testSetup static void setupTestData(){


  static testMethod void testService() {

    RestRequest req = new RestRequest(); 
    req.requestURI = Constants.OWNER_PORTAL_ENDPOINTS.get('testService');  
    req.httpMethod = 'POST';
    Map<Object,Object> requestMap = New Map<Object,Object>{
        'test' => 'test'
    req.requestBody = Blob.valueOf(JSON.serialize(requestMap));
    RestContext.request = req;
    RestResponse response = RestContext.response;



Anyone have any Idea, how to test Rest class written in above pattern.


  • What is the error its throwing currently ? Feb 29, 2016 at 10:24
  • Problem is too weird. In the test class there is a debug message. When I see the debugs after execution of test class, I can not see any debugs. But when I append any string to debug message, it start printing debug message only once.When I run the test class again, I can not see debug message again. The error is null pointer exception in rest class where we are getting parameters for restRequest. Feb 29, 2016 at 10:36
  • First thing to try is to add RestContext.response = new RestResponse(); to your test before the call to your @HttpPost; that will avoid the risk of an NPE at response.addHeader(...).
    – Keith C
    Feb 29, 2016 at 10:42
  • @KeithC but I am not adding any header in the Response. and dont understand that why system.debug() prints only once whenever we change anything into the debug message, and in second run stop printing. Feb 29, 2016 at 10:51
  • @KeithC your suggestion is working. Thanks but I am still not sure what was wrong. Please add your comment as an answer so that I can make it correct. and please add some explanation also about the problem. Feb 29, 2016 at 10:52

3 Answers 3


In unit tests neither RestContext.request or RestContext.response are automatically initialised whereas in the normal execution environment they are. So if your @RestResource class references these values you need to set both of them up in your test.

So add to your test before the call to the @HttpPost method:

RestContext.response = new RestResponse();

so that this line in your @HttpPost method:


does not cause an NPE that breaks your test because response is null.


You might use

System.RestContext.request = new RestRequest();
RestContext.request.requestBody = Blob.valueOf(JSON.serialize(conList)+'<<<<<');

It worked for me...


Here is a good unit test example for apex rest services by Jeff Douglas.


  • 4
    Gabe, please don't just include a link. Since links can go stale and break, we normally quote the relevant parts of the page. Jun 1, 2016 at 19:14

You must log in to answer this question.

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