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
prasanth sfdcprasanth sfdc 

Test class error for simple rest api class

Hi,
    I have written rest api code (http Get) method for capturing lead information from external system. I have rested the api, it is working good. But duing the test class i am getting NULL values for parameters. Kindly help me on this. 

Apex class:-  

In the below class, "allvalue" varible is showing as null null .......  in debug log.  Please help me on this. 
 
@RestResource(urlMapping='/timeemailer/*')
global class timeemailerRestApi{

    @HttpGet
    global static String createNewLead()
    {
    string erroMsg = '';
    string allvalue ='';
    try{
     RESTRequest req = RESTContext.Request;
     RESTResponse res = RESTContext.Response;
  //  String body =  req.requestBody.Tostring();
    
    String name = req.params.get('name');
    string emailid = req.params.get('emailid');
    string  Phonenumber = req.params.get('Phonenumber');
    string Projectname = req.params.get('Projectname');
    string key = req.params.get('key');
    
    allvalue = name +'..'+emailid+'...'+phonenumber+'....'+projectname+'..'+key;
    
    system.debug('The coming values are  : '  + allvalue);
   
   if(key!= null && key.Equals(label.Times_E_mailer_key)) 
   { 
    lead l1 = new lead();
    l1.lastname = name;
    if(emailid != null && emailid !='NA')
    l1.Email = emailid;
    l1.mobilephone = phonenumber;
    l1.projects__c = Projectname;
    l1.company ='NA';
    l1.Source_Channel__c = 'Times-E-mailer';
    l1.run_lead_assignment__c =true;
    
    insert l1; 
    
    erroMsg = 'Record sucessfully inserted in salesforce. ';
   } 
   else{
     erroMsg = 'You have entered a wrong API key';
      Error_Log__c el = new Error_Log__c();
           el.Class_Trigger_Name__c ='timeemailerRestApi ';
           el.Error_Description__c = 'Sent wrong key ......................................... The original sent data is :'  + allvalue;
           el.Method_Name__c = 'lead insert method';
           insert el;           
   }
    }
      catch(exception ert)
      {
         Error_Log__c el = new Error_Log__c();
           el.Class_Trigger_Name__c ='timeemailerRestApi ';
           el.Error_Description__c = string.valueof(ert)+'......................................... The original sent data is :'  + allvalue;
           el.Method_Name__c = string.valueof(ert.getLineNumber());
           insert el;           
         erroMsg = 'Records are not inserted ' + string.valueof(ert);
      }
      
    
    return erroMsg;
  
 
    
    }
    
    }

TEST CLASS:-
 
@istest
private class timeemailerRestApi_test{

@istest private static void testmethod1()
{

  


   // req.requestURI = '/services/apexrest/timeemailer';  //Request URL
  //  req.addParameter('name','hellotesting');
  //  req.addParameter('emailid','testing123123get@gmail.com');
  //  req.addParameter('Phonenumber','9842011142');
  //  req.addParameter('Projectname','Acropolis');
  //  req.addparameter('Key','Times-E-mailer');
  
  
   
   
    RestRequest req1 = new RestRequest(); 
    RestResponse res1 = new RestResponse();

    // pass the req and resp objects to the method     
    req1.requestURI = '/services/apexrest/timeemailer?key=Times-E-mailer&name=TestingGetmethod&emailid=testing123123get@gmail.com&Phonenumber=9842011142&Projectname=myprojectName';  //Request URL
    req1.httpMethod = 'GET';
    
    RestContext.request = req1;
    RestContext.response = res1;

 


   string responseMsg = timeemailerRestApi.createNewLead(); //Call the Method of the Class with Proper 


}
}

 
Amit Singh 1Amit Singh 1
Hi Prashanth,

Have you tried with addPeremeters method of RestRequest Class? That must do the trick.

Regards,
Amit
prasanth sfdcprasanth sfdc
earlier i have added addParameter, same error is coming. No i have changed to addParameters, now class is not saving. Below is the error. 

"Error: Compile Error: Method does not exist or incorrect signature: void addPeremeters(String, String) from the type System.RestRequest at line 22 column 9"
Amit Singh 1Amit Singh 1
Try code
@istest
private class timeemailerRestApi_test{

@istest private static void testmethod1()
{
    RestRequest req = new RestRequest(); 
    RestResponse res = new RestResponse();

    // pass the req and resp objects to the method     
    req.requestURI = '/services/apexrest/timeemailer';  //Request URL
    req.httpMethod = 'GET';
	req.addParameter('name','hellotesting');
	req.addParameter('emailid','testing123123get@gmail.com');
	req.addParameter('Phonenumber','9842011142');
	req.addParameter('Projectname','Acropolis');
	req.addparameter('Key','Times-E-mailer');
    
    RestContext.request = req;
    RestContext.response = res;
   string responseMsg = timeemailerRestApi.createNewLead(); //Call the Method of the Class with Proper 
 }
}

 
prasanth sfdcprasanth sfdc
No change in code coverage.  Please find the below screens.  Null vlaue is coming.  Thank you very much for reply. 

User-added imageUser-added imageUser-added image
Amit Singh 1Amit Singh 1
Strange can you please debug the params which is the Map and see what is the output of the Map?