- Any test class should be annotated with @isTest annotation.
- Test method will have an identifier as testmethod as part of the method declaration.
- To test Rest api RestRequest and RestResponse object to be instantiated and added in RestContext instance
- Invoke Rest Api class to get back the response.
Example shown below:
@isTest
private class RestApiSample_Test {
static testMethod void testGetMethod() {
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/RestApiSample';
req.addParameter('userName', 'Raghav');
req.httpMethod = 'GET';
RestContext.request = req;
RestContext.response = res;
String msg = RestApiSample.getWelcomeMessage();
system.assertEquals(msg, 'Welcome Raghav');
}
}