2 replaced http://salesforce.stackexchange.com/ with https://salesforce.stackexchange.com/
source | link

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 answerAdrian Larson's answer

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

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
source | link

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