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
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Test Class for Apex HTTP Web service

Hi Everyone,
           I have this apex rest web service , that returns the parametres of record when an external application sends the Id through the URL.

Apex Class:
@RestResource(urlMapping='/v1/cans/*')
global with sharing class MyRestResource {
    @HttpGet
    global static CanWrapper doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        CanWrapper response= new CanWrapper();
        String CanId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        List<CAN__c> result = [SELECT Awarded_Project__c, Opportunity__r.Proposal__c,Opportunity__r.Account.Name, Opportunity__r.Master_IDIQ_Project__c FROM CAN__c WHERE Id = :CanId];
        if(result.size()>0){
            response.canrecord=result[0];
            response.status='Success';
            response.message='Callout Sucess';
        }
        else{
            response.status='Fail';
            response.message='No Record Found';        
        }
        return response;
        
    }
    global class CanWrapper {
          public CAN__c canrecord;
          public String status;
          public String message;
    }
}

Apex Test
@isTest
private class Test_MyRestResource{



  static testMethod void testDoGet() {

    RestRequest req = new RestRequest(); 
    RestResponse res = new RestResponse();

    req.requestURI = 'https://cs11.salesforce.com/services/apexrest/v1/cans/'+'a0DZ000000V5SjqMAF';  
    req.httpMethod = 'GET';
    RestContext.request = req;
    RestContext.response = res;
    Test.startTest();
    MyRestResource.CanWrapper results = MyRestResource.doGet();
    Test.stopTest();

    System.assertEquals('Success', results.status);
    System.assertEquals('Callout Sucess', results.message);

  }

}


Even though I am giving a legit Id in my test class , it is not returning a record. It is returning null  and the method fails. Because of that if(result.size()>0)  condition is not getting evaluated to True. Can anyone please help me with this???


Thanks a lot for your time

Shrey Tyagi
 
Best Answer chosen by shrey.tyagi88@tcs.com
@Karanraj@Karanraj
Hi Shrey,

The problem here is you can't access org data in your test class. For any test class, you must create test data in your test class method. Check out this Apex test class Trailhead module https://developer.salesforce.com/trailhead/module/apex_testing which helps you write a proper test class

All Answers

RohRoh
Hello Shrey,
Salesforce today provides a HTTPMockCallout Class to test our service.
Please go through the below documentation, hope that helps.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm

PLEASE SELECT THIS AS THE ANSWER, IF YOU LIKE IT.

Thanks,
Rohit Alladi
@Karanraj@Karanraj
Hi Shrey,

The problem here is you can't access org data in your test class. For any test class, you must create test data in your test class method. Check out this Apex test class Trailhead module https://developer.salesforce.com/trailhead/module/apex_testing which helps you write a proper test class
This was selected as the best answer
ram @SFDCram @SFDC
Hi Roh, what is the neccessity of implementing mock interface here. The documents are saying that we need to implement the mock interfaces for setting up fakeresponses right. Here Shrey is getting request and passing response, then why he need to implement fake responses. The thing is if he set fake request parameters then the test class handles the response itself. Its like a test class what we are implementing a test class for a normal apex class. You can refer trailhead  link below https://trailhead.salesforce.com/en/modules/apex_integration_services/units/apex_integration_webservices .