Activity Forums Salesforce® Discussions How do you create a test class for a callout in Salesforce?

  • How do you create a test class for a callout in Salesforce?

    Posted by Anuj on September 4, 2020 at 3:52 pm

    How do you create a test class for a callout in Salesforce?

    Aditya replied 2 years ago 2 Members · 1 Reply
  • 1 Reply
  • Aditya

    Member
    September 5, 2020 at 8:04 am

    <pre id="ext-gen27">@isTest
    global class MockHttpResponseGenerator implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
    // Optionally, only send a mock response for a specific endpoint
    // and method.
    System.assertEquals('http://example.com/example/test', req.getEndpoint());
    System.assertEquals('GET', req.getMethod());

    // Create a fake response
    HttpResponse res = new HttpResponse();
    res.setHeader('Content-Type', 'application/json');
    res.setBody('{"example":"test"}');
    res.setStatusCode(200);
    return res;
    }
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos