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
Luke Higgins 23Luke Higgins 23 

Writing a test class for an HttpPost

I need help on writing a test class for the following- 
@RestResource(urlMapping='/approve/*')
global with sharing class exAppClass {
  jstcl__TG_Timesheet__c ts = new jstcl__TG_Timesheet__c();
        // GET request
       @HttpPost
        global static String doGet() {
        RestRequest req = RestContext.request;   
          String secid = req.params.get('id');
          String approverId = req.params.get('appid');
          String ipString = req.params.get('ip');
          System.debug(secid);
          String alreadyApproved = 'expired';
          String error = 'error';
          String success = 'approved';
          String primApprover;
          String secApprover;
          jstcl__TG_Timesheet__c ts;
          ts = [SELECT Id, jstcl__Week_Ending__c, jstcl__Status__c, jstcl__Placement__r.jstcl__SecondaryApprover__c, jstcl__Placement__r.jstcl__TimecardApprover__c FROM jstcl__TG_Timesheet__c WHERE Id = :tsid FOR UPDATE];
          primApprover = String.valueOf(ts.jstcl__Placement__r.jstcl__TimecardApprover__c);
          secApprover = String.valueOf(ts.jstcl__Placement__r.jstcl__SecondaryApprover__c);  
          if (ts != null && ts.jstcl__Status__c == 'Approved' && (primApprover == approverId || secApprover == approverId )) {
            return alreadyApproved;
          } else if(ts != null && ts.jstcl__Status__c == 'Submitted' && (primApprover == approverId || secApprover == approverId)){
            try{
                insertLog(ts, approverId, ipString);
                ts.jstcl__Status__c = 'Approved';
                update ts;   
            } catch(Exception e) {
                System.debug('Exception caught: ' + e.getMessage()); 
                return error;   
            } 
          return success;
          }else {
          return error;
          }
        }
     
        private static Express_Approval_Log__c insertLog(jstcl__TG_Timesheet__c ts, Id approverId, String ip){
          Express_Approval_Log__c log = new Express_Approval_Log__c();
          log.Name = 'EAL '+ts.jstcl__Week_Ending__c;
          log.Timesheet__c = ts.Id;
          log.Approval_Date_Time__c= DateTime.now();
          log.Status__c = 'Approved';
          log.Timesheet_Approver__c = approverId;
          log.IP_Address__c = ip;
         try{
            insert log;
         }catch(Exception e) {
            System.debug('Exception caught: ' + e.getMessage());    
        } 
          return log;
      }
}

Test class so far:

@isTest
private class exAppClassTest {
    static testMethod void  updateMethodTest(){
     jstcl__TG_Timesheet__c ts = createTestRecord();
        
     String JSONMsg = '{"id" : "'+ts.Id +'","ip":"12:34:00:00","appid":"a4t3700000345ZC4AAO}';

     RestRequest req = new RestRequest();
     RestResponse res = new RestResponse();
     
     req.requestURI = '/services/apexrest/abc/xyz/';  //Request URL
     req.httpMethod = 'POST';//HTTP Request Type
     req.requestBody = Blob.valueof(JSONMsg);
     
     RestContext.request = req;
     RestContext.response= res;
     
     Test.startTest();
         exAppClass.doGet();
     Test.StopTest();   
 }
 
 // Helper method to create test data
 static jstcl__TG_Timesheet__c createTestRecord() {
     // Create test record
     Contact con=new Contact(
        FirstName='fname',
        LastName = 'lname',
        Email = 'email@gmail.com',
        Phone = '9743800309'); 
    insert con; 
    ts2__Placement__c plc = new ts2__Placement__c();
    plc.jstcl__SecondaryApprover__c = con.Id;
    plc.jstcl__TimecardApprover__c = con.Id;
    insert plc;
    jstcl__TG_Timesheet__c ts =new jstcl__TG_Timesheet__c ();
    date mydate = date.parse('12/27/2009');
    ts.jstcl__Status__c='Test';
    ts.jstcl__Week_Ending__c=mydate ;
    insert ts;
    Express_Approval_Log__c log = new Express_Approval_Log__c();
    log.Timesheet__c = ts.Id;
    log.name = 'Test';
    log.Timesheet_Approver__c = con.Id;
    log.Status__c = 'Approved';
    insert log;
     return ts ;
 }
}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Luke,

You can follow the below documentation that you could use for reference to write test classes for your implementation.

>> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing.htm

I hope this helps and in case if this comes handy can you please choose this as best answer so that it can be useful for others in the future.

Regards,
Anutej