How to write test class for Apex Rest Services ?

A Developer wants to push the rest service to production which requires a code coverage as per the deployment strategy and best practices of salesforce. In this scenario, we must create a test class to test the rest services

Rest Service Class:-

@RestResource(urlMapping='/Account/*')
global with sharing class AccountService {
  
    @HttpGet
    global static Account doGet() {
        RestRequest req = RestContext.request;
        String acctId = req.requestURI.substring(req.requestURI.lastIndexOf('/') + 1);
        Account result = [SELECT Id, Name FROM Account WHERE Id =: acctId];
        return result;
    }
  
    @HttpPost
    global static String doPost(String name, String descrp) {
        Account a = new Account(Name = name, Description = descrp);        
        insert a;
        return a.Id;
    }

    @HttpDelete
    global static void doDelete() {
        RestRequest req = RestContext.request;
        String memberId = req.requestURI.substring(req.requestURI.lastIndexOf('/') + 1);
        Account memb = [SELECT Id FROM Account WHERE Id = :memberId];
        delete memb;
    }
    
}

in above code, the method doGet() returns the Account record given by account ID in the parameter of rest context which is tested in below test class:-

Test Class:-

In the below code, the RestRequest and RestResponse are used to store the account id and rest service URl to make calls to the rest service. The request parameter  ‘requestURI’ holds the URL along the with parameter as account id.  The  httpMethod describes the http method like GET, POST, PUT, DELETE etc.

@isTest
private class AccountServiceTest {

    @testSetup
    static void dataSetup() {
        Account acc = new Account(Name = 'Testing');
        insert acc;
    }

    static testMethod void testGet() {
        Account acc = [ SELECT Id FROM Account LIMIT 1 ];
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();             
        req.requestURI = '/services/apexrest/AccountService/' + acc.Id;
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response= res;
        Account acctResp = AccountService.doGet();
        system.assertEquals(acctResp.Name, 'Testing');
    }
    
    static testMethod void testPost() {
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();             
        req.requestURI = '/services/apexrest/AccountService/';
        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response= res;
        String acctId = AccountService.doPost('Test', 'Testing');
        Account acc = [ SELECT Id, Name, Description FROM Account WHERE Id =: acctId ];
        system.assertEquals(acc.Name, 'Test');
        system.assertEquals(acc.Description, 'Testing');
    }
    
    static testMethod void testDelete() {
        Account acc = [ SELECT Id FROM Account LIMIT 1 ];
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();             
        req.requestURI = '/services/apexrest/AccountService/' + acc.Id;
        req.httpMethod = 'DELETE';
        RestContext.request = req;
        RestContext.response= res;
        AccountService.doDelete();
        system.assertEquals( [ SELECT COUNT() FROM Account ], 0);
    }
    
}


 

Practice on Rest Services using trailhead here…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s