0

I needed to throw a callout exception by hardcoding it in test class for batch class.Is there any method to do that ?

try{
   XXXX
} catch (CalloutException excpObj) {
                insertInterfaceTrace(accObj.Id,' Callout Exception','---- ERROR = '+ excpObj.getMessage(),accObj);
}

The catch block needs to be covered in test class.But don't know how to hardcode a callout exception in catch block

1
  • 1
    Are you able to provide any code, or explain in more detail exactly what you're having difficulty with? Sep 26, 2014 at 11:08

1 Answer 1

1

You can generate exception by:

//System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
public static testmethod void testConstructor()    {

       Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
       Test.startTest();
           Account acc = new Account(Name='Test Ignore'); // any sobject for DML
           insert acc;
           /* Replace me with execute batch statement here in between DML */
           delete acc;
       Test.stopTest();
 }

Copied from: Test a catch block for callout exception

You must log in to answer this question.

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