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
JoshTonksJoshTonks 

Test Class Help With RestAPiCallout

Hi,

Would anybody please be able to give me assistance on this.
Ive written difference test classes before for different things but i dont know how to do this one. It sends a json api call out to an external service
 
public class RAMAssistSetup {
    @InvocableMethod(label='RAM Assist Setup ' description='This sets up ram assist')
    public static void RAMAssistSetup(List<Id> FleetAssist) {
        List<Fleet_Assist__c> RFA =[Select Id from Fleet_Assist__c where id in :FleetAssist ];
        System.enqueueJob(new RestApiCall(FleetAssist));
    }
    public class RestApiCall implements System.Queueable, Database.AllowsCallouts {
        List<Id> RFA = new List<Id>() ; 
        public RestApiCall(List<Id> ids){
            RFA = ids ;
        }
        public void execute(System.QueueableContext ctx) {
            Fleet_Assist__c FA = [Select Id ,Contact_Email__c,Company__c,Contact_Full_Name__c,Company_Name_Text__c,Contact_Telephone__c,fm_Licenses__c,fm_Address__c from Fleet_Assist__c where id = :RFA limit 1] ;
            JSONGenerator gen = JSON.createGenerator(true);    
            gen.writeStartObject();      
            gen.writeStringField('email',FA.Contact_Email__c);
            gen.writeStringField('sfId',FA.Company__c);
            gen.writeStringField('name',FA.Contact_Full_Name__c);
            gen.writeStringField('companyName',FA.Company_Name_Text__c);
            gen.writeStringField('tel',FA.Contact_Telephone__c);
            gen.writeStringField('address',FA.fm_Address__c);
            gen.writeStringField('licenses',FA.fm_Licenses__c);
            gen.writeEndObject();    
            String jsonS = gen.getAsString();
            System.debug('jsonMaterials'+jsonS);

            // Sending the http body with JSON 

            String endpoint = 'https://api.xxxxxxxx.com/xxxxxx';
            HttpRequest req = new HttpRequest();
            req.setEndpoint(endpoint);
            req.setMethod('POST');
            req.setHeader('Content-Type','application/json;charset=UTF-8');
            req.setbody(jsonS);
            Http http = new Http();
            HTTPResponse response = http.send(req);
            // Parse the JSON response
            if (response.getStatusCode() != 200) {
                System.debug('The status code returned was not expected: ' +
                    response.getStatusCode() + ' ' + response.getStatus());
            } else {
                System.debug(response.getBody());
            }
            
         }
    }
}
Im assuming looking at it there are 2 test classes for it one for the top section and then one for the restapicall.

Do i need to write a mock httpcallout method for this and will i need to run a record create testmethod for this so it can find the object

Thanks in advance


 
Christan G 4Christan G 4
Hi Tonsky, I hope you are well. Yes, you'll most likely have to create a mock httpcallout method to test this as well as other components. For more information, I suggest reviewing the following:

https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_webservices
https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_queueable#Tdxn4tBK-heading2