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
SANDEEP CHITTINENISANDEEP CHITTINENI 

Test Class for @HttpGet method. Can you please help me out with this? I'm getting an error "Test methods do not support webservice callouts"

Please help me guys. I appreciate your help.
@HttpGet
    global static Account getAccountById() {
        RestRequest request = RestContext.request;
        // grab the caseId from the end of the URL
        String accountId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Account result =  [SELECT Id,Name FROM Account WHERE Id = :accountId];
        return result;
    }      
    @HttpDelete
    global static void deleteAccount() {
        RestRequest request = RestContext.request;
        String accountId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Account thisAccount = [SELECT Id FROM Account WHERE Id = :accountId];
        delete thisAccount;
    }

Thanks in advance!
Sandeep Chittineni
Best Answer chosen by SANDEEP CHITTINENI
Meghna Vijay 7Meghna Vijay 7
Hi, 
Here's a link to it:-
https://salesforce.stackexchange.com/questions/173928/code-coverage-for-httpget-rest-method/173975
Hope it helps.
Thanks

All Answers

Meghna Vijay 7Meghna Vijay 7
Hi,
Create HTTPCalloutMock class to create a fake response and use it in your testMethods.
Here's a link to it:-
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
Hope it helps, if it does, mark it as solved to keep the community clean.
Thanks
SANDEEP CHITTINENISANDEEP CHITTINENI

Thanks for your time Meghna! Yes we can use HTTPCalloutMock interface to create a fake cresponse but I need a fake request this is a rest api used to request from external application. Any other suggestions meghna?

Thanks for you once again!
Sandeep Chittineni

Meghna Vijay 7Meghna Vijay 7
Hi, 
Here's a link to it:-
https://salesforce.stackexchange.com/questions/173928/code-coverage-for-httpget-rest-method/173975
Hope it helps.
Thanks
This was selected as the best answer
SANDEEP CHITTINENISANDEEP CHITTINENI

Thanks Meghna! This might be best solution for other developers so that they can find easily. I tried to implement this in my code. And it is throwing an error "Test methods do not support webservice callouts".

Thank you very much for the help Meghna!

Meghna Vijay 7Meghna Vijay 7
Can you post your test class code snippet ?
 
SANDEEP CHITTINENISANDEEP CHITTINENI
Here it is...

//Webservice...
@RestResource(urlMapping='/Account/*')
global with sharing class webService_To_POST_Data {
    @HttpPut
    global static Account getPatientDataAsJSON(Id accId, String name, String cec,Date dob, String address1, String address2, Id owner, String phone, String email){
        Account a = new Account();
        a.Id = accId;
        a.Name = name;
        a.CEC_Number__c = cec;
        a.Date_of_Birth__c = dob;
        a.ShippingState = address1;
        a.ShippingPostalCode = address2;
        //a.OwnerId = owner;
        a.Phone = phone;                              
        a.Email__c = email;       
        upsert a;
        system.debug('Name ='+name+' CEC = '+cec);
        return a;
    }
    @HttpGet
    global static Account getAccountById() {
        RestRequest request = RestContext.request;
        // grab the caseId from the end of the URL
        String accountId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Account result =  [SELECT Id,Name FROM Account WHERE Id = :accountId];
        return result;
    }      
    @HttpDelete
    global static void deleteAccount() {
        RestRequest request = RestContext.request;
        String accountId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Account thisAccount = [SELECT Id FROM Account WHERE Id = :accountId];
        delete thisAccount;
    }        
}

//And Test Class...
@isTest
private class webServiceToPOSTData_TEST {   
    static testmethod void testAccount(){  
        //ARRANGE - setup request like the external system would....
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        webService_To_POST_Data classToTest = new webService_To_POST_Data();
        
        Account acc = new Account();
        acc.Name = 'Test';
        insert acc;
        req.requestURI = 'https://cs84.salesforce.com/services/apexrest/Account/'+acc.id;
        
        req.addHeader('Content-Type', 'application/json');
        req.httpMethod = 'GET';
        req.requestBody = Blob.valueof('{}');
        
        RestContext.request = req;
        RestContext.response= res;
        
        
        //ACT - make the request from inside the test execution context, rather than from the external system
        Test.startTest();
        webService_To_POST_Data.getAccountById();
        Test.stopTest();
  }
}

 
Meghna Vijay 7Meghna Vijay 7
Strange it's working in my dev org and giving a code coverage of 28% as the test method is only for GET.
 
SANDEEP CHITTINENISANDEEP CHITTINENI
Seriously, Did the test passed?
Meghna Vijay 7Meghna Vijay 7
Yes it did. 
SANDEEP CHITTINENISANDEEP CHITTINENI
User-added image

This is very strange. My test method is failed and I'm getting this error all the time.
Can you resend back the code you wrote in you console?
Thanks,
Sandee.
Meghna Vijay 7Meghna Vijay 7
@RestResource(urlMapping='/Account/*')
global with sharing class webService_To_POST_Data {
    @HttpPut
    global static Account getPatientDataAsJSON(Id accId, String name, String cec,Date dob, String address1, String address2, Id owner, String phone, String email){
        Account a = new Account();
        a.Id = accId;
        a.Name = name;
       
        a.ShippingState = address1;
        a.ShippingPostalCode = address2;
        //a.OwnerId = owner;
        a.Phone = phone;                              
      
        upsert a;
        system.debug('Name ='+name+' CEC = '+cec);
        return a;
    }
    @HttpGet
    global static Account getAccountById() {
        RestRequest request = RestContext.request;
        // grab the caseId from the end of the URL
        String accountId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Account result =  [SELECT Id,Name FROM Account WHERE Id = :accountId];
        return result;
    }      
    @HttpDelete
    global static void deleteAccount() {
        RestRequest request = RestContext.request;
        String accountId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Account thisAccount = [SELECT Id FROM Account WHERE Id = :accountId];
        delete thisAccount;
    }        
}

// Test Class
@isTest
public class RestTestClass {
	
  
    static testmethod void testAccount(){  
        //ARRANGE - setup request like the external system would....
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        webService_To_POST_Data classToTest = new webService_To_POST_Data();
        
        Account acc = new Account();
        acc.Name = 'Test';
        insert acc;
        req.requestURI = 'https://cs84.salesforce.com/services/apexrest/Account/'+acc.id;
        
        req.addHeader('Content-Type', 'application/json');
        req.httpMethod = 'GET';
        req.requestBody = Blob.valueof('{}');
        
        RestContext.request = req;
        RestContext.response= res;
        
        
        //ACT - make the request from inside the test execution context, rather than from the external system
        Test.startTest();
        webService_To_POST_Data.getAccountById();
        Test.stopTest();
  }

}

It's the same code which you posted previously.
 
SANDEEP CHITTINENISANDEEP CHITTINENI
Yes meghna. It's quite same. But I how come the error is throwing for me? I don't know. Anyways, I appreciate your time and help.
Thanks,
Sandeep
Meghna Vijay 7Meghna Vijay 7
I removed CEC , DOB and Email as i didn't have them in my org.
 
SANDEEP CHITTINENISANDEEP CHITTINENI
I understood that.
SANDEEP CHITTINENISANDEEP CHITTINENI
Code is getting coverage I got 25% for that method but the method failed.
Meghna Vijay 7Meghna Vijay 7
Not in my case. Not getting any error in Test Logs.
 
SANDEEP CHITTINENISANDEEP CHITTINENI
Ok Thanks for the help! I hope see you again and resolving your issues in the forums