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 sfdxsalesforce sfdx 

how to write an Test Class for an functionality of REST API

@RestResource (urlMapping='/DeviceObjserviceendpoint/*')
global class DeviceObj_Rest_Service {
  
    @HttpGet
    global static void doGetDevice() {
        String result = '';
        try{
        Map<string, string> paramsMap = RestContext.request.params;
      if(paramsMap!=null){
        String serialNo = paramsMap.get('serialNo');
        String lastName = paramsMap.get('lastName');
        String dateOfBirth = paramsMap.get('dateOfBirth');  
                 Date dob = date.parse(dateOfBirth);
            
       
        //Date dob = date.parse(dateOfBirth);
        List<Device__C> Devs = [Select Id, Name,Dob__c,Patient_Last_Name__c from Device__C 
                                where Name = :SerialNo and  
                                Patient_Last_Name__c =:lastName and 
                                DOB__c = :dob
                                LIMIT 1];

         if( Devs.size() > 0 ) {
             Device__C Dev = Devs[0];
            Map<String,String> resultMap = new Map<String,String> {
                'Message'  => 'Device found Sucessfully',
                'DeviceId' => Dev.Id,
                'SerialNo' => Dev.Name,
                'DateOfBirth' => string.valueof(Dev.Dob__c),
                 'lastname' =>Dev.Patient_Last_Name__c
            };
           
          result = JSON.serialize( resultMap );
       
         }
        else {
            result = 'Device with SerialNo :' + SerialNo + ' not found!';
        }
        }
        }catch(Exception ex){
            result= ex.getMessage();
        }
        RestContext.response.addHeader('Content-Type','application/json');
        RestContext.response.responseBody = Blob.valueOf(result);
    
         }
    
}

My Test Class:
@isTest
private class DeviceObj_Rest_Service_Test {
    
    static testMethod void testDoGetDevice() {
         Id stExam = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Patient').getRecordTypeId();
         system.debug(stExam);
         
         Account acc = new Account(FirstName = 'Test', LastName = 'Test22', PersonEmail = 'testaccount@test.com', RecordTypeId = stExam);
         insert acc;  
            
       
        // create a new Device record
        Device__c device = new Device__c(Name = '1234567890', type__c='Test',Patient__c= acc.id);
        insert device;
        
        // test a valid GET request
        RestRequest request = new RestRequest();
        request.requestURI = '/services/apexrest/DeviceObjserviceendpoint';
        request.addParameter('serialNo', device.Name);
        request.addParameter('lastName', device.Patient_Last_Name__c);
        request.addParameter('dateOfBirth', String.valueOf(device.DOB__c));
        RestContext.request = request;
        
        Test.startTest();
        DeviceObj_Rest_Service.doGetDevice();
        Test.stopTest();
        
        RestResponse response = RestContext.response;
      
    }
    
}

Its getting failed due to some issues , please provide some tips or any links to refer .

Thanks
Prathusha Reddy.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Prathusha,

Can you confirm what were the errors that you are facing. Can you share those .

Thanks,
 
salesforce sfdxsalesforce sfdx
  Hi Sai Praveen, Firstly thank you for your reply.

System.NullPointerException: Attempt to de-reference a null object.

//Class.DeviceObj_Rest_Service.doGetDevice: line 49, column 1
   RestContext.response.addHeader('Content-Type','application/json');
//Class.DeviceObj_Rest_Service_Test.testDoGetDevice: line 25, column 1
 DeviceObj_Rest_Service.doGetDevice();

can you please suggest to me if anything I am missing
Thanks
prathusha
Vineeth ReddyVineeth Reddy
Can you copy paste the complete stack trace here ? The class is only 48 lines long and the null pointer reported is on line 49. Something is missing here. Copy and paste the class, test class and complete stack trace, should be something trivial.