salesforce

In This post I will explain you how to write test class for apex rest resources . so here i have four class .

  • BookingWebService  : Rest Resource class for inserting lead
  • BookingResponseWrapper : wrapper class for holding  our rest resource return values we will use wrapper class to hold success and failure data.
  • BookingWrapper : wrapper class for holding input values parameter for rest resource.
  • BookingWebService_TC : Test class for Rest class

BookingWebService

This is the Rest Resource class we are creating to insert lead data .

So here is the code :

@RestResource(urlMapping='/CreatingleadFromSite/*')
global class BookingWebService {
    @HttpPost
    global static BookingResponseWrapper createBooking(BookingWrapper Wrapper) {
        BookingResponseWrapper brwObj  = new BookingResponseWrapper(true,'');
        Savepoint sp = Database.setSavepoint(); 
        try{
            lead ld = new lead();
            ld = wrapper.ld;
            if(ld !=null){
                insert ld;
            }
            brwObj.isSuccess =True;
            brwObj.message = 'Record Inserted Succesfully';
        }
        catch(exception e){
            Database.rollback(sp);
            brwObj.isSuccess =False;
            brwObj.message = e.getMessage();
        }
        return  brwObj;
    }
}

BookingResponseWrapper:

This class holds the return values for  our restresource class . this class holds two variables  issuccess and message. when lead inserted succesfully we set this values to success otherwise in case of exception this will return error message.

 

global class BookingResponseWrapper {
    Public Boolean isSuccess;
    Public string  message;
    
   global  BookingResponseWrapper(Boolean isSuccess,string  message){
        this.isSuccess = isSuccess;
        this.message = message;
    }
}

BookingWrapper:

This Class used to Hold the data for our BookingWebService class parameter . webservice get the input from wrapper class and on the basis of this input create lead.

global class BookingWrapper {
    public Lead ld;
    
    
  global  BookingWrapper(lead ld){
        this.ld = ld;
       
    }
}

BookingWebService_TC:

test class for our rest resource class,

@istest
public class BookingWebService_TC {
    
    public static testMethod void  Test1(){
        
        
        lead ld = new lead(lastname = 'test',company ='testcompany');
        BookingWrapper Wrapper = new BookingWrapper(ld); 
        
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/CreatingBookingFromSite'; 
        req.httpMethod = 'Post';
        req.addHeader('Content-Type', 'application/json'); 
        RestContext.request = req;
        RestContext.response = res; 
        Test.startTest();
       		 BookingWebService.createBooking(Wrapper);
        Test.stopTest();  
    }
    public static testmethod void catchException(){
        
        lead ld = new lead(lastname = 'test');
      
        BookingWrapper Wrapper = new BookingWrapper(ld); 
        
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/CreatingBookingFromSite'; 
        req.httpMethod = 'Post';
        req.addHeader('Content-Type', 'application/json'); 
        RestContext.request = req;
        RestContext.response = res;
        Test.startTest();
       		 BookingWebService.createBooking(Wrapper);
        Test.stopTest();  
    }
}

 

Hits: 1266

Share Post

By Himanshu Rana

My Name is Himanshu Rana, 23 Years young, born and grow up in Ghaziabad, India. A High Spirited Salesforce Admin, Developer and a Blogger. I currently work at Wakencode Technologies,

Leave a Reply

Your email address will not be published. Required fields are marked *