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
shailendra singhaishailendra singhai 

webservices test class need to create

Could anyonehelp me to create test class for below, iamnot getting the expected test coverage.

@RestResource(urlMapping='/LeadRestGet/*')

global with sharing class NS_WS_Lead_Insert {
    @HttpGet
    global static Lead doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        //String leadId = req.params.get('existing_lead_id');
        String leadId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Lead result = [SELECT Id, FirstName, LastName, phone, Email, Company,  Lead_Source_Information__c,
         Authorization_to_contact_card_provider__c          
        FROM Lead WHERE Id = :leadId];
        return result;
        
    }
    
    @HttpPost
    global static String doPost()
    {
        RestRequest req = RestContext.request;
        RestResponse res = Restcontext.response;
        System.debug('Params: ' + req.params);
        //String temp = req.requestBody.toString();
        Lead ll = new Lead();
        ll.FirstName = req.params.get('first_name');
        ll.LastName = req.params.get('last_name');
        ll.phone = req.params.get('phone');
        ll.Email = req.params.get('email');
        ll.Web_to_Lead__c = Boolean.valueOf(req.params.get('web_to_lead'));
        //if (req.params.get('web_to_lead') == 'true'){
        //    ll.Web_to_Lead__c = true;
        // }
               
        string messagesent;     
         try {
            insert ll;
        }
        catch (DMLException e)
        {
            messagesent=e.getMessage();
        }
        if(messagesent==null)  
        {
            messagesent=ll.id;
        }
        return messagesent;
    }
}
shailendra singhaishailendra singhai
Hi Gokula, thanks for your suggestion.
but the below code works for me:
static testMethod void testdoPost() {
RestRequest req = new RestRequest();
        req.addHeader('Content-Type', 'application/json'); // Add a JSON Header as it is validated
        req.requestURI = '/services/apexrest/LeadRestGet/';
        req.httpMethod = 'POST';
       
        req.addParameter('first_name','Shail');
        req.addParameter('last_name','Sashi');
        req.addParameter('phone','020202020200');
        req.addParameter('email','ss@ssho.com');
        req.addParameter('company_name','Myown company');
        req.addParameter('registration_number','3456256');
        req.addParameter('company_status','active');
        req.addParameter('company_type','limited');
        req.addParameter('incorporated_date','2016-12-12');
       
       // req.requestBody = Blob.valueof(JSONMsg); // Add JSON Message as a POST
       
        RestResponse res = new RestResponse();
        RestContext.request = req;
        RestContext.response = res;
        XXXXX_Lead_Insert.doPost();
    }