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

Write a test class for Http Post when returning a String

I have an Http post that I can't seem to write a test class for. Most of the documentation surrounds the class returning an HttpResponse. I have mine returning a String.

Apex class:
@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;
      }
}

 
AbhishekAbhishek (Salesforce Developers) 
Luke,

Try the suggestions as mentioned in the below,

https://developer.salesforce.com/forums/?id=9060G000000XdDoQAK

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.