Activity Forums Salesforce® Discussions How can I call a mock class in test class in salesforce?

  • Yogesh

    Member
    October 17, 2019 at 7:10 am

    Hello,

    If you have specified the values of the fake response, instruct the Apex runtime to send this fake response by calling Test.setMock in your test method. For the first argument, pass HttpCalloutMock.class, and for the second argument, pass a new instance of your interface implementation of HttpCalloutMock, as follows:

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

  • Saddam

    Member
    October 17, 2019 at 7:28 am

    Hi,

    Testing HTTP Callouts by Implementing the HttpCalloutMock Interface
    Provide an implementation for the HttpCalloutMock interface to specify the response sent in the respond method, which the Apex runtime calls to send a response for a callout.

    global class YourHttpCalloutMockImpl implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest req) {
    // Create a fake response.
    // Set response values, and
    // return response.
    }
    }

    Now that you have specified the values of the fake response, instruct the Apex runtime to send this fake response by calling Test.setMock in your test method. For the first argument, pass HttpCalloutMock.class, and for the second argument, pass a new instance of your interface implementation of HttpCalloutMock, as follows:

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

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos