Tuesday, May 17, 2016

APEX: Creating Tests for Rest Services in Apex using GET

Suppose you have the following rest service in your org:

@HttpGet global static Object getObject() { Map<String, String> paramMap = RestContext.request.params; String param1Value = paramMap.get('param1'); }

How do you test the above code snippet?
In your test class, you need to provide the parameters as follows:

// Initialize both response and request from the service to be called
RestRequest rReq = new RestRequest(); RestResponse rRes = new RestResponse(); // The Service we want to access rReq.requestURI = url.getSalesforceBaseUrl().toExternalForm() + '/MyRestServiceURL/'; // The parameter(s) we want to pass to the service rReq.addParameter('param1', 'some value'); // Service method rReq.httpMethod = 'GET'; // Service Context objects ready RestContext.request = rReq; RestContext.response = rRes; // Call Service Object myObj = MyRestService.getObject();

No comments:

Post a Comment