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
agarciaagarcia 

Test class for public static void InvocableMethod

I am trying to write a test class for the code below.  Can anyone help??  The (List<String> SubmitData) passes json formatted string which comes from a flow(example below).  


public class ASGITS {
@InvocableMethod(label='Send ASG-ITS')
    public static void sendASGITS (List<String> SubmitData){
        
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        String payload = SubmitData.get(0);
        req.setEndpoint('https://secure.sample.com/test/manage.svc/PostNewExam');
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/json');
        req.setBody(payload);  
        HttpResponse res = http.send(req);
if (res.getStatusCode() != 200) {
    System.debug('The status code returned was not expected: ' +
        res.getStatusCode() + ' ' + res.getStatus());
} else {
    System.debug(res.getBody());
}               
}
}



Sample Code the flow is passing to the variable "SubmitData"
{"ExamType":"ORTHOTICS","PatientFirstName":"{!First_Name}","PatientLastName":"{!Last_Name}","PatientDob":"{!Date_of_Birth}","PatientSex":"{!M_F_Convert}","PatientHomePhone":"{!Phone}","PatientAddress1":"{!Street}","PatientCity":"{!City}","PatientState":"{!State}","PatientPostalCode":"{!Zip_Code}"}
 
Raj VakatiRaj Vakati
Hi   agarcia,

Here is the code .

@isTest
global class ASGITS_test implements HttpCalloutMock {
    global static testmethod void case1(){
        Test.startTest() ;
        ASGITS.sendASGITS(new List<String>{'Test Data'});
        Test.stopTest() ;
        
    }
    
    global HTTPResponse respond(HTTPRequest req) {
        // Optionally, only send a mock response for a specific endpoint
        // and method.
        System.assertEquals('http://api.salesforce.com/foo/bar', req.getEndpoint());
        System.assertEquals('GET', req.getMethod());
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"ExamType":"ORTHOTICS","PatientFirstName":"{!First_Name}","PatientLastName":"{!Last_Name}","PatientDob":"{!Date_of_Birth}","PatientSex":"{!M_F_Convert}","PatientHomePhone":"{!Phone}","PatientAddress1":"{!Street}","PatientCity":"{!City}","PatientState":"{!State}","PatientPostalCode":"{!Zip_Code}"}');
        res.setStatusCode(200);
        return res;
    }
    
}
agarciaagarcia
Rajamohan.Vakati, 
At first I thought it worked, I got 90% code coverage with the error "Methods defined as TestMethod do not support Web service callouts".
I figured 90% was enough for now and I could clean up the test and push changes later.  HOWEVER, when I send the change set, it wont pass, see below.   Let me know if you can help....THANK YOU!!!!

User-added image


User-added image