+ Start a Discussion
NareshKrishnaNareshKrishna 

Test Method for RESTService Calls

Hi,

 

Can anyone please help me on how to write Test Method in apex class in which I am calling the REST Service.

 

When the corresponding service is called from the testMethod, the code below the service call is not complied i.e the code is appearing in blue color upto

"JSONObject resp = this.getJSONObject('http://localhost:8080/service/testService.json', 'GET');   "

and the rest of the code is in red color.

 

Please help me on this.

 

Thanks in advance.



Best Answer chosen by Admin (Salesforce Developers) 
joshbirkjoshbirk

Since test methods can't call out to web services, you need to code it in such a way that you can give your Apex some test data.  Some people solve this by having a global boolean which flags that a test is being run, and then put the callout code in one conditional block, and another with the test data. 

 

Make sense or need some sample code?