@RestResource(urlMapping='/myrest2/*')
global with sharing class Rest_Class2
{
//method will take ID from URL and display the details
@HTTPGet
global static Account[] doGet()
{
Account[] acts = new Account[]{};
acts=[select name,type,industry,phone from Account];
return acts;
}
}
Test class.
@isTest
public class Rest_Class2Test
{
static testMethod void testMethodForAll()
{
Account testAccount = new Account();
testAccount.Name='Test Account' ;
testAccount.Industry='Bank';
insert testAccount;
Test.startTest();
RestResponse res = new RestResponse();
RestRequest req = new RestRequest();
req.httpMethod = 'Get';
req.addHeader('Content-Type', 'application/json'); // Add a JSON Header as it is validated
req.requestURI = '/services/apexrest/myrest2/all' ;
RestContext.request = req;
RestContext.response = res;
List<Account> lstAcc = Rest_Class2.doGet();
System.assertEquals(lstAcc.size() ,1);
Test.stopTest();
}
}
Test class.
Let us know if this will help you
Thanks
Amit Chaudhary
If you want to access the REST API in Salesforce from an external system, please follow this link: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/
If you want to let other systems access Salesforce, please check the code provided by Amit.
If you just want to see how this works in Salesforce, you can access the workbenc for your org: https://workbench.developerforce.com/login.php