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
Krista KellyKrista Kelly 

Test class for External call out

I have a Batch class below (also a scheduled class). I am trying to write the test class using a HTTPCalloutMock but am really struggling). Example I'm trying to follow below but I don't have an getInfoFromExternalService() in MyBatchClass.

Any advice would be appreciated. Thank you.

global class MyBatchClass implements Database.Batchable<sObject>,Database.AllowsCallouts, Database.Stateful {
    global Database.QueryLocator start(Database.BatchableContext bc) {
        System.debug('START METHOD');
        String Query= 'SELECT salesforceid__c,Id, number_of_services_in_use__c FROM Account WHERE salesforceid__c != null';
            return Database.getQueryLocator(Query);
    global void execute(Database.BatchableContext bc, List<Account> records){
        System.debug('EXECUTE METHOD');
        // process each batch of records
        String Token ='eyJhbGciOiJSUzI1NiIsImtpZCI6Im1hYXNfcHJvZF8yMDIwMDMyNiIsInR5cCI6IkpXVCJ9.eyJvcmciOiJzYWxlc2ZvcmNlIiwib3JnVHlwZSI6IkVOVEVSUFJJU0UiLCJzdWIiOiJyd3Jqdzl2ZHU2cCIsInBlcm1pc3Npb25zIjoiQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUJBPSIsImFwaVRva2VuSWQiOiJqZW44bjN3aGthciIsImlzcyI6IlNvbGFjZSBDb3Jwb3JhdGlvbiIsImlhdCI6MTU5ODI5MDg3N30.FPfTJFwPfhW3qrEMCcjcfNj-SO8IDMo7MiurTqFcTu2GIStNAjVcjFHD1ESS_3Feo95n6Se_u7ifyee3uLwTTZJknskFn9FB-1ZCFzo0jRKz6TigIzc7WEpFPvbZt4L_MmvLa7eSqM5ovewt9nAtQ1ttJFm7a_35lMsLQu5Bf3f-WbuCjE-POzFnBWASQetCj63TAdgkYu1wNPqZmavJESLlg6Wv6oWf2H5HI_m9fW7Vt2TypQuhIz0Q25iFQoYPKWmseDxU-EUQIwJ1tSqYSuyDNWpFDhEqwTAyBLz4cu-8a-m29caaeG0eIWxIydcMmxq68THY4KTn9Rtmxy_Yug';
        // Initiating the http request
        Http http1 = new http();
        HttpRequest request = new HttpRequest();
        // Adding the paramters for the request
        request.setHeader('Authorization', 'Bearer '+Token);
        Httpresponse response1 = http1.send(request);
        system.debug('Data response= '+response1.getBody());
        //create a list so we do one insert for all the Metrics records
        List<Cloud_Usage_Metrics__c> newMetrics = new List<Cloud_Usage_Metrics__c>();
        // Logic to map and update the account object
        if (response1.getStatusCode() == 200) {
            // Deserialize the JSON string into collections of primitive data types.
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response1.getBody());
            // Cast the values in the 'accountCloudMetrics' key as a list
            List<Object> data = (List<Object>) results.get('data');
            System.debug('Received the following metrics:');
            for (Object metric: data) {
                Map<String, Object> i = (Map<String, Object>)metric;
               for(Account a: records){
                        if (a.salesforceid__c == i.get('salesforceId')) {
                            Cloud_Usage_Metrics__c met = new Cloud_Usage_Metrics__c(
            //insert newMetrics(met);
        } else {
            System.debug('Callout failed: ' + response1);
        insert newMetrics;
        update records;
    global void finish(Database.BatchableContext bc){ }  

ANUTEJANUTEJ (Salesforce Developers) 
Hi Krista,

Can you try checking the below implementation that could help you as a reference to write a test class for your use case.