Activity Forums Salesforce® Discussions How can we manually cover the catch block(in try catch block) in apex test class?

  • Abhinav

    Member
    July 22, 2016 at 3:34 pm

    Hi Mohit,

    To cover the catch block you have to throw the exception in your test class. Sample for the callout exception.

    System.CalloutException in your mock:

    @TestVisible class UnauthorizedEndpointResponse implements HttpCalloutMock {
    public HttpResponse respond(HttpRequest request) {
    CalloutException e = (CalloutException)CalloutException.class.newInstance();
    e.setMessage('Unauthorized endpoint, please check Setup->Security->Remote site settings.');
    throw e;
    }
    }

    Then cause it to appear in your test with the normal HTTP mocks:

    Test.setMock(HttpCalloutMock.class, new UnauthorizedEndpointResponse());
    BatchClass.doTheCallout();

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos