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
aaryansriaaryansri 

Test class for webservices

Hi ,


  I need to write test class for webservice code can some body help on that below is the class code and purpose of the class

Class used to get component record ID to be used in custom buttons without hardcode.

Global without sharing class GetComponentId{   
WebService static string GetIDbyComponentName(String ComponentName, String ComponentType) {        
   if(ComponentType == 'Report'){         
   String ComponentId = [SELECT Id FROM Report WHERE DeveloperName = :ComponentName LIMIT 1].Id;          
  return ComponentId;      
}else
{            String ComponentId = 'error';   
         return ComponentId;       }}}
Ankit AroraAnkit Arora
This is just to get you started, I hope you can take care of rest.

@isTest
private class testMyWebService
{
  static testMethod void testMyWebSvcForIf()
  {
      GetComponentId.GetIDbyComponentName act = new GetComponentId.GetIDbyComponentName('DeveloperNameOfYourReport' , 'Report');
  }
  static testMethod void testMyWebSvcForElse()
  {
      GetComponentId.GetIDbyComponentName act = new GetComponentId.GetIDbyComponentName('AnyString' , 'NotReport');
  }
}