+ Start a Discussion

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.setHeader('Content-Type', 'application/json');
        HttpResponse res = http.send(req);
if (res.getStatusCode() != 200) {
    System.debug('The status code returned was not expected: ' +
        res.getStatusCode() + ' ' + res.getStatus());
} else {

Sample Code the flow is passing to the variable "SubmitData"
Raj VakatiRaj Vakati
Hi   agarcia,

Here is the code .

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');
        return res;
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