9

Alright, I have a HTTP callout that every once in a while would timeout, even with the max timeout set. So to combat this, I have a try, catch, try, catch block to make the call out a second time if there was no response. I know how to test the callout responses implementing HttpCalloutMock, however, what I do not know, it how to test the timeout. I have tried returning a null value, and blank values, this just gives me errors. How do I go about testing for a timed out response call to a server? I am assuming I have to throw a salesforce timeout error at it, but how? You can not test a callout without a mock, and all mocks must return something, or so it seems? Please and thank you for any help anyone can provide.

Code block I am trying to test the catch on, as stated it only goes to the catch if salesforce does not get a response before timing out:

try {
        //Execute web service call here     
        res = h.send(req); 
        //debug messages
        //system.debug(res.toString());
        system.debug('Response: '+ res.getBody());  
        if(res.getStatusCode() ==200){success=true;}
        else{
            r.Broker_Error_Message__c = 'STATUS CODE: ' + res.getStatusCode() + ' STATUS: ' + res.getStatus();
            r.Broker_Status__c = 'Submission Failed';
            upsert r;
            return;
        }
    } 
    catch(System.CalloutException e) {
        try{
            system.debug('CalloutException: '+e);
            res = h.send(req); 
            //debug messages 
            System.debug(res.toString());
            System.debug('STATUS: '+res.getStatus());
            System.debug('STATUS_CODE: '+res.getStatusCode());
            if(res.getStatusCode() ==200){success=true;}
            else{
                r.Broker_Error_Message__c = 'STATUS CODE: ' + res.getStatusCode() + ' STATUS: ' + res.getStatus();
                r.Broker_Status__c = 'Submission Failed';
                upsert r;
                return;
            }
        }
        catch(System.CalloutException ex) {
            system.debug('CalloutException: '+ex);
            r.Broker_Error_Message__c = 'STATUS CODE: ' + res.getStatusCode() + ' STATUS: ' + res.getStatus();
            r.Broker_Status__c = 'Submission Failed';
            upsert r;
            return;
        }
    }

1 Answer 1

6

When you write your implementation of HttpCalloutMock, use a constructor argument that tells the mock to throw a callout exception

For example

Test.setMock(HttpCalloutMock.class,new MyMock(true)); // throwException
Test.setMock(HttpCalloutMock.class,new MyMock(false)); //  no Exception

with your mock class looking like:

public class MyMock {
    private Boolean throwCalloutException;
    public MyMock(Boolean throwCalloutException) {  // constructor
      this.throwCalloutException = throwCalloutException;
    } 
    public HttpResponse respond(HttpRequest hReq) {
      if (this.throwCalloutException) {
         CalloutException e = new CalloutException();
         e.setMessage('This is a constructed exception!');
         throw e;
      }
      else {
        ... setup normal HttpResponse object and return
      }
}

This builds on the shoulders of Adrian Larson's answer

1
  • Thank you very much, this worked for me, I appreciate your time. Apr 20, 2016 at 12:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .