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
SUMAN KUMARI 70SUMAN KUMARI 70 

Create an Apex REST service that returns an account and its contacts. 1

My test class is failing stating that "Methods defined as TestMethod do not support Web service callouts". Please find my classes below:-

@RestResource(urlMapping='/Accounts/*/contacts')
global class AccountManager {
    @HttpGet
    global static Account getAccount() {
        RestRequest req = RestContext.request;
        String accId = req.requestURI.substringBetween('Accounts/', '/contacts');
        Account acc = [SELECT Id, Name, (SELECT Id, Name FROM contacts) 
                       FROM Account WHERE Id = :accId];
        return acc;
    }
}




@isTest
private class AccountManagerTest {
@isTest
    private static  void testGetAccount(){
        Id recordId = createTestRecord();
        RestRequest request = new RestRequest();
        request.requestUri = 'https://sumankumari360-dev-ed.my.salesforce.com/services/apexrest/Accounts/'+recordId+'/contacts';
        request.httpMethod = 'GET';
        RestContext.request = request;
        Account thisAccount = AccountManager.getAccount();
        System.assert(thisAccount != null);
        System.assertEquals('Test record', thisAccount.Name);
    }
    static Id createTestRecord(){
         Account TestAcc = new Account(
          Name='Test record');
        insert TestAcc;
        Contact TestCon= new Contact(
        LastName='Test', 
        AccountId = TestAcc.id);
        return TestAcc.Id;
    }      
}
    
}
VinayVinay (Salesforce Developers) 
Hi Suman,

You need to implement a WebserviceMock and create a fake response.
Also, the starttest and stoptest should be around your future callout method, which will in turn invoke the Mockcallout implementation to generate a fake response.

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

References with similar error:
https://salesforce-season.blogspot.com/2017/03/methods-defined-as-testmethod-do-not.html
https://salesforce.stackexchange.com/questions/31808/method-defined-as-testmethod-do-not-support-web-service-callouts-test-skipped#:~:text=This%20error%20occurs%20because%20web,to%20bypass%20web%20services%20Callout.&text=The%20mock%20class%20will%20take%20care%20of%20webservice%20callouts.

Thanks,
Vinay Kumar