Salesforce – Mock callouts in Apex

Reading Time: < 1 minutes

      
        private class RestMock implements HttpCalloutMock {

        public HTTPResponse respond(HTTPRequest req) {
            HTTPResponse res = new HTTPResponse();
            res.setHeader('Content-Type', 'text/json');
            res.setBody('{}');
            res.setStatusCode(200);
            return res;
        }
    }
      
    
      
    @isTest static void myTest() {
        Test.setMock(HttpCalloutMock.class, new RestMock());
        
        // Some stuff...

        Test.startTest();
        // Test-related stuff...
        Test.stopTest();
        
    }
      
    

Photo by Angèle Kamp on Unsplash


About the author

Andrés Canavesi
Andrés Canavesi

Software Engineer with 15+ experience in software development, specialized in Salesforce, Java and Node.js.


Related posts


Leave a Reply

%d bloggers like this: