+ Start a Discussion
AntonPavlovAntonPavlov 

write test class for htttp request

Help to write test class for htttp request  I i can't figure it out
public  with sharing class currencyRates {
    @AuraEnabled
    public static List<Object> getRates(){
        
        String requestData='https://www.nbrb.by/api/exrates/rates?periodicity=0';
        
        Http http =new Http();
        HttpRequest request = new HttpRequest();
        request.setEndPoint(requestData);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        List<Object> result =null;
        if(response.getStatusCode()==200){
            result =(List<Object>)JSON.deserializeUntyped(response.getBody());
        }else{
            ApexPages.Message mes =new ApexPages.Message(ApexPages.Severity.ERROR,'There was an error in reading Data');
        }
        return result;
    }
}
Best Answer chosen by AntonPavlov
Maharajan CMaharajan C
Hi Uladzimir,

1. Create a new mock apex class for httprequest:
 
@isTest
global class currencyRatesMock implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest request) {
         HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('[{"Cur_ID":130,"Date":"2020-12-31T00:00:00","Cur_Abbreviation":"CHF","Cur_Scale":1,"Cur_Name":"Швейцарский франк","Cur_OfficialRate":2.9147}]');
        response.setStatusCode(200);
        return response;
    }
}

2. Create the below test class:
 
@isTest
public class currencyRatesTest {
    @isTest static void getRatesTest(){
        Test.setMock(HttpCalloutMock.class, new currencyRatesMock());
        List<Object> objList = currencyRates.getRates();
        system.assert(!objList.isEmpty());
    }
}

Thanks,
Maharajan.C

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Uladzimir,

>> https://salesforce.stackexchange.com/questions/273737/rest-callout-test-class-how-to-cover-auraenabled-method

The above link has an example of a test class for an auraenabled class with HTTP callout can you try implementing a test class and see if it works?

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Maharajan CMaharajan C
Hi Uladzimir,

1. Create a new mock apex class for httprequest:
 
@isTest
global class currencyRatesMock implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest request) {
         HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('[{"Cur_ID":130,"Date":"2020-12-31T00:00:00","Cur_Abbreviation":"CHF","Cur_Scale":1,"Cur_Name":"Швейцарский франк","Cur_OfficialRate":2.9147}]');
        response.setStatusCode(200);
        return response;
    }
}

2. Create the below test class:
 
@isTest
public class currencyRatesTest {
    @isTest static void getRatesTest(){
        Test.setMock(HttpCalloutMock.class, new currencyRatesMock());
        List<Object> objList = currencyRates.getRates();
        system.assert(!objList.isEmpty());
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
AntonPavlovAntonPavlov
Thaks you all you helped a lot. I figure it out.