How to mock callouts in Salesforce Apex

How to mock callouts in Salesforce Apex
Reading Time: 2 minutes

No, you cannot make any callouts in Apex unit tests. Even if one day is possible, it is generally recommended to avoid making actual callouts to external services during unit testing. Instead, you should mock the callouts to ensure isolation, control, efficiency, and determinism in your tests.

Mocking callouts means simulating the responses you expect from the callouts without actually making the network requests. This approach helps you maintain consistent test results and reduces the reliance on external services during testing.

Mocking callouts in Apex is important for several reasons:

  1. Isolation: By mocking callouts, you can isolate your Apex code from external dependencies. This means that your tests will not rely on external services or APIs, ensuring that they can be executed consistently, regardless of the availability or stability of those external services.
  2. Control: Mocking allows you to have complete control over the responses received from the callouts. You can simulate different scenarios and responses, such as successful responses, error responses, or even timeouts, to thoroughly test how your code handles different situations.
  3. Efficiency: Avoiding actual callouts during testing improves the efficiency of your test suite. Callouts involve network requests, which can be time-consuming and add unnecessary overhead to your tests. Mocking allows your tests to run faster since the responses are generated locally without the need for network communication.
  4. Deterministic Testing: Mocking enables you to have predictable and deterministic tests. By defining the exact responses your code will receive, you can ensure consistent test results every time you run your tests, regardless of external factors.
  5. Error Handling: Mocking callouts allows you to easily test and verify how your code handles various error scenarios. You can simulate different error conditions and ensure that your code behaves correctly by handling the errors and executing the appropriate error-handling logic.

Overall, mocking callouts in Apex provides a controlled and efficient way to test your code, helping you identify and fix issues early on, while ensuring the reliability and stability of your application.

To mock callouts in Apex unit tests, you can use the built-in Apex mocking framework. By implementing the HttpCalloutMock interface and defining the desired response in the respond method, you can simulate callout responses.

Here’s an example to illustrate the process:

public class MockCallout implements HttpCalloutMock {
    public HttpResponse respond(HttpRequest req) {
        // Create and return the desired response data
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"example": "mock response"}');
        return res;

public class MyCalloutClassTest {
    static void testCallout() {
        // Set the mock callout
        Test.setMock(HttpCalloutMock.class, new MockCallout());
        // Start test context
        // Call the method that makes the callout
        MyCalloutClass myClass = new MyCalloutClass();
        // Stop test context
        // Assert the results
        // ...

In this example, the MockCallout class implements the HttpCalloutMock interface to simulate the expected response. The MyCalloutClassTest test class sets the mock callout using Test.setMock and then calls the method that makes the callout. This enables you to test the behavior of your code without actually making the callout.

By following this approach, you can effectively mock callouts and perform thorough testing of your Apex code while maintaining control, efficiency, and deterministic results.

, ,

About the author

Andrés Canavesi
Andrés Canavesi

Software Engineer with 15+ experience in software development, specialized in Salesforce, Java and Node.js.

Join 22 other subscribers

Leave a Reply