You need to sign in to do that
Don't have an account?

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.
TEST CLASS:-
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 } }
Have you tried with addPeremeters method of RestRequest Class? That must do the trick.
Regards,
Amit
"Error: Compile Error: Method does not exist or incorrect signature: void addPeremeters(String, String) from the type System.RestRequest at line 22 column 9"