Skip to main content
Hi All,

I'm trying to add another trigger but I'm getting only 72% coverage.  The trigger is only trying to do a callout to post some data to another server.

I picked up on using HttpCalloutMock but I keep getting the error "salesforce Methods defined as TestMethod do not support Web service callouts Stack Trace: null".  

As far as I can understand with the pattern, if I call Test.setMock(HttpCalloutMock.class, new FutureClassCalloutMock()) before my regular callout, it will call the mock method instead.  Obviously I'm doing something wrong because it's not working.

Any ideas? I'm just ripping my hair out on this and the documentation is not helping.

CallbackProcessor.PostData is the line in the test class below where it seems to be failing.

@isTest

private class futureCls_Test {  

 private static testMethod void srvcCallout_Test() {        

    Test.startTest();

    // Unit test to cover http web service

    // CallbackProcessor.PostData(url, data); 

    Test.setMock(HttpCalloutMock.class, new FutureClassCalloutMock()); 

    

    string url = 'https://xpresstaxappeals.com/callback/WebhookHexRefunds';

    CallbackProcessor.PostData(url, 'Just random testing data'); 

        

    // Unit test to cover future method

    //futureCls.srvcCallout(oppId, oppAmt,oppType);    

    Test.stopTest();

  }

}

I've got the mock interface.

global interface HttpCalloutMock {

    void PostData(string url, HTTPRequest request);

}

And here is my mock future class.

@isTest

global class FutureClassCalloutMock implements HttpCalloutMock {

   global void PostData(string url, HTTPRequest req) {

      System.debug('In FutureClassCalloutMock.respond() method.');

      HTTPResponse res = new HTTPResponse();

      // doesn't do anything since it's a Post.

   }

}
1 answer
  1. Jul 5, 2018, 9:42 PM
    Try this, get rid of the mock interface you created. I am referring to:

    global interface HttpCalloutMock {

        void PostData(string url, HTTPRequest request);

    }

    Then change FutureClassCalloutMock to look like this

    @isTest

    global class FutureClassCalloutMock implements HttpCalloutMock {

    global HTTPResponse respond(HTTPRequest request) {

    // Create a fake response

    HttpResponse response = new HttpResponse();

    response.setHeader('Content-Type', 'application/json');

    response.setBody('{"testing": ["test1", "test2"]}');

    response.setStatusCode(200);

    return response;

    }

    }

    And then assuming that CallbackProcessor is similar to this

    public class CallbackProcessor {

    public static HttpResponse PostData(string url, String testing) {

    Http http = new Http();

    HttpRequest request = new HttpRequest();

    request.setEndpoint(url);

    request.setMethod('POST');

    request.setHeader('Content-Type', 'application/json;charset=UTF-8');

    request.setBody('{"testing":"the test"}');

    HttpResponse response = http.send(request);

    // Parse the JSON response

    if (response.getStatusCode() != 201) {

    System.debug('The status code returned was not expected: ' +

    response.getStatusCode() + ' ' + response.getStatus());

    } else {

    System.debug(response.getBody());

    }

    return response;

    }

    }

    The test class becomes

    @isTest

    private class futureCls_Test {

    private static testMethod void srvcCallout_Test() {

    // Unit test to cover http web service

    // CallbackProcessor.PostData(url, data);

    Test.setMock(HttpCalloutMock.class, new FutureClassCalloutMock());

    string url = 'https://xpresstaxappeals.com/callback/WebhookHexRefunds';

    HttpResponse response = CallbackProcessor.PostData(url, 'Just random testing data');

    System.assertEquals(200, response.getStatusCode());

    System.assertEquals(response.getBody(), '{"testing": ["test1", "test2"]}');

    // Unit test to cover future method

    //futureCls.srvcCallout(oppId, oppAmt,oppType);

    }

    }

     
0/9000