Test Class for Apex rest services is not getting parameters

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.



Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

You might use

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

It worked for me…

Method 3

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x