Salesforce - Mock callouts in Apex

How to use mockups to tests callouts in Apex


Feb 19, 2021
featured image
      
        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


salesforce apex dev testing