1

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

@RestResource(urlMapping='/ownerPortal')
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);
    }set;}
    @HttpPost
    global static void  handleRequest() {
        RestResponse response = RestContext.response;
        try{
            response.addHeader('Content-Type','application/json');
            RestRequest req = RestContext.request;
            Map<String,String> headersMap = req.headers;
            if(!req.params.containsKey('action')){
                throw New CustomException('action parameter is required!');
            }
            if(String.isBlank(headersMap.get('CRS_Authorization'))){
                throw New CustomException('CRS authorization header missing!');
            }
            String action = req.params.get('action');
            if(TEST_SERVICE.equalsIgnoreCase(action)){
                result = Rest_Services_Owner_Portal_Handler.testService(req.requestBody.toString());
            }else{
                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){

        System.debug(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

@isTest
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';
    req.addHeader('CRS_Authorization','00DO000000537j1!ARsAQHvz9MMOxZ24dez0hCYWl0_WpITR1kIoOv3wo67zV6a48Y0RYN9FEzPQf.73.7TZGrVEnq_9X5zLGryMAVVNnQO4jehD');
    Map<Object,Object> requestMap = New Map<Object,Object>{
        'test' => 'test'
    };
    req.addParameter('action','testService');
    req.requestBody = Blob.valueOf(JSON.serialize(requestMap));
    RestContext.request = req;
    System.debug(req);
    Rest_Services_Owner_Portal.handleRequest();
    RestResponse response = RestContext.response;
    System.debug(response.responseBody.toString());

  }

}

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

Thanks

  • What is the error its throwing currently ? – karthikselva Feb 29 '16 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. – ashishcloud Feb 29 '16 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 '16 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. – ashishcloud Feb 29 '16 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. – ashishcloud Feb 29 '16 at 10:52
5

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:

response.addHeader('Content-Type','application/json');

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

0

You might use

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

It worked for me...

-1

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

http://blog.jeffdouglas.com/2012/03/21/writing-unit-tests-for-v24-apex-rest-services/

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

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.