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
Shaan Khokhar 9Shaan Khokhar 9 

How to write a test class for a class that has multiple callouts

Hi all,

I am curious how i'd make a test class for the below code. It gets the api from https://api.exchangeratesapi.io/latest.

You can read it below:

 

global class updateCurrency Implements Schedulable{
    global void execute(SchedulableContext sc)
       {
            getCurrencies();
       }
    public static HttpResponse httpRequest(){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://api.exchangeratesapi.io/latest?base=GBP');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        system.debug('Http request'+response.getBody());
        return response;
    }
  @future(callout=true)  public static void getCurrencies(){
        Id usdID, eurID, cadId;
        double usd, eur, cad;
        HttpResponse response = httpRequest();
        if (response.getStatusCode() == 200) {
            // Deserialize the JSON string into collections of primitive data types.
            Map<String,Object> jsonParsed =(Map<String,Object> ) JSON.deserializeUntyped(response.getBody());
            Map<String,Object> ratesParsed = ( Map<String,Object>) jsonParsed.get('rates');
            usd = (double) ratesParsed.get('USD');
            eur = (double) ratesParsed.get('EUR');
            cad = (double) ratesParsed.get('CAD');
        }
        List<CurrencyType> listCurrencies = new List<CurrencyType>();
        listCurrencies = [SELECT Id,IsoCode FROM CurrencyType WHERE IsActive = true];
        if(listCurrencies.size() > 0){
            for(integer i = 0; i < listCurrencies.size(); i++){
                if(listCurrencies[i].IsoCode == 'USD'){
                    usdID = listCurrencies[i].Id;
                    setCurrency(usdID, usd);
                }
                else if(listCurrencies[i].IsoCode == 'EUR'){
                    eurID = listCurrencies[i].Id;
                    setCurrency(eurID, eur);
                }
                else if(listCurrencies[i].IsoCode == 'CAD'){
                    cadID = listCurrencies[i].Id;
                    setCurrency(cadID, cad);
                }
            }              
        }
    }
    
    public static HttpResponse setCurrency(Id curId, double rate){
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v51.0/sobjects/CurrencyType/'+curId);
        req.setBody('{ "DecimalPlaces" : 2 , "ConversionRate" :'+rate+'}');
        req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
        req.setHeader('Content-Type', 'application/json');
        req.setMethod('PATCH');
        HttpResponse res = h.send(req); 
        system.debug('Request Body' + res);
        return res;
    }
}
ShirishaShirisha (Salesforce Developers) 
Hi Shaan,

Greetings!

Please find the sample test class code in the below thread:

https://salesforce.stackexchange.com/questions/228369/how-to-write-a-test-class-for-dynamically-running-multiple-callouts

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri