function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Salesforce Dev199Salesforce Dev199 

TEST class for Rest API

Can anyone help me how to write a test class for REST API? below is my code.. Please help guys.
@RestResource(urlMapping='/Course__c/*')
global class WebServiceExpose
{
    
    public String Name;
    public String sectionKey;
    public String profkey;
    public String schoolKey;
    public String Group1;
    public String coursecreated;
    public String startDate;
    public String endDate;
    public Double Prod_Capsim360;
    public Double Prod_GlobalDNA;
    public static WebServiceExpose parse(String Json)
    {
        return (WebServiceExpose) System.JSON.deserialize(json, WebServiceExpose.class);
    }
    
    @HttpPost
    global static ID doPost()
    {
        RestRequest req = RestContext.request;
        Blob body = req.requestBody;
        String requestString = body.toString();
        
        WebServiceExpose we= (WebServiceExpose)JSON.deserialize(requestString, WebServiceExpose.class);
        System.debug('-->>'+we);
        String Name1=we.Name;
        String sectionKey1=we.sectionKey;
        String profkey1=we.profkey;
        String Group1=we.Group1;
    	Date coursecreated=Date.valueOf(we.coursecreated);
        Datetime startDate=Date.valueOf(we.startDate);
        Datetime endDate=Date.valueOf(we.endDate);
        String schoolkey1=we.schoolKey;
        Double Prod_Capsim360c=we.Prod_Capsim360;
        Double Prod_GlobalDNA1=we.Prod_GlobalDNA;
        List<Contact> con=new List<Contact>();
		con=[select id, Professor_Key__c,Contact.AccountId from Contact where Professor_Key__c=: profkey1 LIMIT 1];
		System.debug('-'+con);       
        List<AccountContactRelation> accon=new List<AccountContactRelation>();
        accon=[select contactId, accountId,account.School_Key__c from AccountContactRelation where account.School_Key__c=: schoolkey1];
        
        
        Course__c course=new Course__c(Name=Name1, Contact__c=con[0].Id, Account__c=accon[0].AccountId, Group__c= Group1, Course_Created_Date__c=coursecreated,End_Date__c= endDate, Start_Date__c=startDate, Prod_Capsim360__c= Prod_Capsim360c,Prod_GlobalDNA__c=Prod_GlobalDNA1,Section_Key__c=sectionKey1);
		insert course;
        System.debug('Course details '+course);
        return course.Id;
    }
    
   
    //public String sectionKey;
    public String studentcount;
    
     @HttpPatch
//Method to update Course fields
global static ID updateRecord()
{
    RestRequest req = RestContext.request;
    Blob body = req.requestBody;
    String requestString = body.toString();
    
    WebServiceExpose we= (WebServiceExpose)JSON.deserialize(requestString, WebServiceExpose.class);
    String sectionKey1=we.sectionKey;
    String studentcount1=we.studentcount;
    
    List<Course__c> course1= new List<Course__c>();
    course1=[select id, Section_Key__c, Number_of_Students__c From Course__c where Section_Key__c= :sectionKey1 ];
    
    course1[0].Number_of_Students__c=Integer.valueOf(studentcount1);
    update course1;
    
    
    return course1[0].id;
}

    
}

 
Best Answer chosen by Salesforce Dev199
AnudeepAnudeep (Salesforce Developers) 
Here is an example that you can refer to

Apex 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;
    }
    
}

Test Class
 
@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);
    }
    
}

If you find this information helpful, please mark this answer as Best. It may help others in the community

Thanks, 
Anudeep