{"id":2504,"date":"2026-11-02T13:00:00","date_gmt":"2026-11-02T07:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/"},"modified":"2026-11-15T22:00:31","modified_gmt":"2026-11-15T16:30:31","slug":"apex-stub-api-mocking","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/","title":{"rendered":"Apex Stub API for Mocking in Unit Tests Explained"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The Apex Stub API lets a test replace one class with a fake version, so the code under test runs without inserting records, querying the database or making a callout. You decide what each method returns, and the test runs in milliseconds.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This fills a real gap. A service class that calculates a fee from three other classes is hard to test honestly, because every test ends up building records for all three. The logic you care about gets buried under setup.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-diagram-1-1024x640.png\" alt=\"Apex Stub API diagram showing the call being intercepted by a stub provider\" class=\"wp-image-2502\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-diagram-1-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Caption: The calling code never knows it is talking to a stub, since the signature is unchanged.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is the Apex Stub API<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Apex Stub API is two pieces. A <code>Test.createStub<\/code> call that builds the fake, and a class implementing <code>System.StubProvider<\/code> that decides what the fake returns.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@IsTest\npublic class FeeServiceMock implements System.StubProvider {\n\n    public Object handleMethodCall(\n        Object stubbedObject,\n        String stubbedMethodName,\n        Type returnType,\n        List&lt;Type&gt; paramTypes,\n        List&lt;String&gt; paramNames,\n        List&lt;Object&gt; args\n    ) {\n        if (stubbedMethodName == 'getFee') {\n            return 4500;\n        }\n        if (stubbedMethodName == 'isOnline') {\n            return true;\n        }\n        return null;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The test then builds the Apex Stub API object and hands it to the code under test.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@IsTest\nstatic void appliesDiscount() {\n    FeeService fake = (FeeService) Test.createStub(\n        FeeService.class,\n        new FeeServiceMock()\n    );\n\n    EnrolmentCalculator calc = new EnrolmentCalculator(fake);\n    Decimal total = calc.totalFor('a01xx');\n\n    System.assertEquals(3825, total);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">No records were created, no SOQL ran, and the test checks exactly one thing. That is the whole point of the Apex Stub API. That is what a unit test should look like.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What the Provider Receives<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Argument<\/th><th>What it holds<\/th><\/tr><\/thead><tbody><tr><td>stubbedObject<\/td><td>The stub itself<\/td><\/tr><tr><td>stubbedMethodName<\/td><td>The method that was called<\/td><\/tr><tr><td>returnType<\/td><td>What the real method returns<\/td><\/tr><tr><td>paramTypes<\/td><td>The parameter types, in order<\/td><\/tr><tr><td>paramNames<\/td><td>The parameter names, in order<\/td><\/tr><tr><td>args<\/td><td>The actual values passed in<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The Apex Stub API passes an <code>args<\/code> list, and that is what makes a provider useful rather than fixed. You can return a different value per input, or record what was called so the test can assert on it afterwards.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (stubbedMethodName == 'getFee') {\n    String courseId = (String) args[0];\n    return courseId == 'a01online' ? 3000 : 4500;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Why the Apex Stub API Matters<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Benefit<\/th><th>What it gives you<\/th><\/tr><\/thead><tbody><tr><td>No DML in tests<\/td><td>A suite that runs in seconds, not minutes<\/td><\/tr><tr><td>Real isolation<\/td><td>A failure points at one class<\/td><\/tr><tr><td>No callout mocks needed<\/td><td>A fake service class instead of a fake HTTP layer<\/td><\/tr><tr><td>Fewer limits used<\/td><td>Tests stop hitting DML and SOQL ceilings<\/td><\/tr><tr><td>Honest coverage<\/td><td>Coverage on logic rather than on setup code<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The speed point compounds. A suite of 400 tests each inserting ten records can take twenty minutes, and a slow suite is a suite people skip. The post on <a href=\"https:\/\/swapnilzambare.com\/blog\/testing-apex-triggers-bulk-negative\/\">testing triggers<\/a> covers where real data is still the right choice.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Rules You Have to Design Around<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Apex Stub API has firm limits, and they shape how you write your classes.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Rule<\/th><th>What it means in practice<\/th><\/tr><\/thead><tbody><tr><td>Only public and protected instance methods can be stubbed<\/td><td>Make the dependency an instance, not a set of statics<\/td><\/tr><tr><td>Static methods cannot be stubbed<\/td><td>A utility class of statics is untestable this way<\/td><\/tr><tr><td>Private methods cannot be stubbed<\/td><td>Test them through the public surface<\/td><\/tr><tr><td>The class cannot be final, and nor can the method<\/td><td>Leave them open<\/td><\/tr><tr><td>System types cannot be stubbed<\/td><td>Wrap them in your own class<\/td><\/tr><tr><td>Only works inside a test context<\/td><td>A stub cannot be created at runtime<\/td><\/tr><tr><td>Constructors do not run<\/td><td>Any setup in the constructor is skipped<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">That first rule is the important one. A class full of static methods cannot be stubbed at all, which is a strong argument for writing services as instances and passing them in.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Dependency Injection Makes It Possible<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public with sharing class EnrolmentCalculator {\n\n    private FeeService fees;\n\n    public EnrolmentCalculator() {\n        this(new FeeService());\n    }\n\n    public EnrolmentCalculator(FeeService fees) {\n        this.fees = fees;\n    }\n\n    public Decimal totalFor(String courseId) {\n        Decimal fee = fees.getFee(courseId);\n        return fees.isOnline(courseId) ? fee * 0.85 : fee;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Two constructors, and the Apex Stub API can reach everything. Writing services this way also keeps them bulk safe, which the post on <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-bulkification-best-practices\/\">bulkification<\/a> covers. Production code uses the empty one, and the test passes a stub into the second. No framework, no configuration, six extra lines.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Apex Stub API vs Real Test Data<\/h2>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-diagram-2-1024x640.png\" alt=\"Apex Stub API and real test data comparison diagram\" class=\"wp-image-2503\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-diagram-2-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Caption: Both belong in a healthy suite, aimed at different things.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>What you are testing<\/th><th>Pick<\/th><\/tr><\/thead><tbody><tr><td>A calculation with several inputs<\/td><td>Stub the dependencies<\/td><\/tr><tr><td>A trigger firing on insert<\/td><td>Real records<\/td><\/tr><tr><td>A validation rule<\/td><td>Real records<\/td><\/tr><tr><td>A service that calls three other services<\/td><td>Stub them<\/td><\/tr><tr><td>A SOQL query shape<\/td><td>Real records<\/td><\/tr><tr><td>Error handling when a dependency throws<\/td><td>Stub, and throw from the provider<\/td><\/tr><tr><td>An end to end flow before release<\/td><td>Real records<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Throwing from a provider is the Apex Stub API case people forget. Mocking the HTTP layer instead is covered in the post on <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-callout-mocks-httpcalloutmock\/\">callout mocks<\/a>. Testing what your code does when a dependency fails is almost impossible with real data and trivial with a stub.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (stubbedMethodName == 'getFee') {\n    throw new CalloutException('Service unavailable');\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">A Real Apex Stub API Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Apex Stub API cut our suite from minutes to seconds. Bright Path Tutors has an enrolment service that reads a fee, checks seat availability through a callout, and applies a discount rule. The original tests inserted courses, built an HTTP mock, and ran for nine seconds each.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Problem<\/th><th>What it cost<\/th><\/tr><\/thead><tbody><tr><td>Records inserted per test<\/td><td>14<\/td><\/tr><tr><td>An HTTP mock class to maintain<\/td><td>Extra code nobody wanted to own<\/td><\/tr><tr><td>Nine seconds per test<\/td><td>A suite nobody ran locally<\/td><\/tr><tr><td>Failures that pointed nowhere<\/td><td>A broken validation rule failed 30 tests<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We split the suite in two. Logic tests use stubs, and a small set of integration tests use real records.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Measure<\/th><th>Before<\/th><th>After<\/th><\/tr><\/thead><tbody><tr><td>Tests in the suite<\/td><td>46<\/td><td>61<\/td><\/tr><tr><td>Suite run time<\/td><td>7 minutes<\/td><td>95 seconds<\/td><\/tr><tr><td>Records inserted across the suite<\/td><td>640<\/td><td>48<\/td><\/tr><tr><td>Tests that failed on one rule change<\/td><td>30<\/td><td>2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">That last row is the real benefit. A failing test now tells you which class is wrong instead of telling you that something, somewhere, changed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Recording Calls for Assertions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An Apex Stub API provider can keep a list of what it was asked, so a test can check that something was called.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public List&lt;String&gt; calls = new List&lt;String&gt;();\n\npublic Object handleMethodCall(Object o, String name, Type rt,\n    List&lt;Type&gt; pt, List&lt;String&gt; pn, List&lt;Object&gt; args) {\n    calls.add(name);\n    return name == 'getFee' ? 4500 : null;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The test then asserts on the list, which is how you prove a cache was used or that a service was not called twice. This is the one capability a hand written fake class gives you less easily.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with the Apex Stub API<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Mistake<\/th><th>What you see<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td>Static methods on the dependency<\/td><td>Nothing can be stubbed<\/td><td>Make the service an instance<\/td><\/tr><tr><td>Expecting the constructor to run<\/td><td>Null fields inside the stub<\/td><td>Return values from the provider, not from state<\/td><\/tr><tr><td>A provider returning the wrong type<\/td><td>A cast error at runtime<\/td><td>Match the declared return type exactly<\/td><\/tr><tr><td>One provider for every class<\/td><td>A long if chain nobody can read<\/td><td>One provider per dependency<\/td><\/tr><tr><td>Stubbing everything<\/td><td>Tests that pass while production fails<\/td><td>Keep integration tests as well<\/td><\/tr><tr><td>Final class or method<\/td><td>A stub creation error<\/td><td>Remove the final keyword<\/td><\/tr><tr><td>Stubbing a system type<\/td><td>A compile or runtime failure<\/td><td>Wrap it in your own class first<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for the Apex Stub API<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Write services as instances with a constructor that accepts dependencies<\/li>\n\n<li>Keep one provider per dependency so each stays readable<\/li>\n\n<li>Return values based on the args list rather than fixed constants<\/li>\n\n<li>Use the provider to throw, so error paths get tested properly<\/li>\n\n<li>Record calls when the test needs to prove something was invoked<\/li>\n\n<li>Keep a small set of integration tests with real records alongside<\/li>\n\n<li>Put providers in test classes or a test only class so they never ship as live code<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Apex Stub API Interview Questions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: What does the Apex Stub API let you do?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Replace a class with a fake inside a test, so no records, queries or callouts are needed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: Which two pieces does it need?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A Test.createStub call and a class implementing the StubProvider interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: What method does StubProvider require?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A handleMethodCall method that receives the call details and returns a value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: What arguments does that method receive?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The stub, the method name, the return type, the parameter types and names, and the actual arguments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: How do you return different values for different inputs?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Read the args list and branch on the values you find there.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: Can you stub a static method?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Only public and protected instance methods can be stubbed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: Can you stub a private method?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Test it through the public surface instead.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: Can a final class be stubbed?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Neither a final class nor a final method can be stubbed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: Can you stub a system type?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Wrap it in your own class and stub that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: Does the real constructor run?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No, which is why a provider should not rely on state set in the constructor.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: Can you create a stub outside a test?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. It works only in a test context.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: Why does this push you toward dependency injection?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because the dependency has to be an instance you can pass in, not a static call.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: How many extra lines does that pattern cost?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Usually a second constructor and a field, so around six lines.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: How do you test an error path with a stub?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Throw the exception from inside the provider for that method name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: Why is that hard with real data?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because making a real dependency fail on demand is awkward and often impossible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: How do you assert that a method was called?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Record the method names in the provider and assert on that list.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: Does using stubs remove the need for integration tests?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. A suite of stubs can pass while the data layer is broken.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: What does a stub do to test run time?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It removes DML and SOQL, which is usually where the time goes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: How does it compare with an HTTP callout mock?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A callout mock fakes the HTTP layer, while a stub fakes your own service class, which is simpler.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: Where should provider classes live?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In test classes or a test only class, so they never deploy as live code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q21: Scenario: createStub throws an error on your class. What is the likely cause?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The class or the method is final, or the method is static or private.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q22: Scenario: the stub returns a value and the code throws a cast error. Why?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The provider returned a type that does not match the declared return type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q23: Scenario: a provider has a 60 line if chain. What went wrong?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One provider is covering several dependencies. Split it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q24: Scenario: a utility class of statics needs testing. What now?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Either test it directly with real inputs, or refactor it into an instance service.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q25: Does a stub count towards code coverage?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The code under test gets coverage, and the provider is test code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q26: Is this the same as Mockito in Java?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is the platform building block such a framework would be built on, with less syntax sugar.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q27: Can the args list be used to validate inputs?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, and asserting on them is a good way to prove a method was called correctly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q28: What is the main design benefit of adopting it?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It pushes you toward small services with explicit dependencies, which are easier to change.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q29: What is the one habit that makes stubs work?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Write every service as an instance and accept its dependencies in a constructor.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q30: Can a stub be passed into a queueable or a batch class?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, if the job accepts the dependency in its constructor, though the stub itself only exists inside the test.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q31: How do you keep providers from growing out of hand?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Give each dependency its own provider and keep the branching to one switch on the method name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q32: What is a reasonable split between stub tests and data tests?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Most of the suite on stubs for logic, with a small set of data tests for triggers, rules and queries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Developer Guide<\/a><\/li>\n\n<li><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/apex_testing\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Testing on Trailhead<\/a><\/li>\n\n<li><a href=\"https:\/\/github.com\/trailheadapps\/apex-recipes\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Recipes<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What Is Next<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, read <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-error-logging-framework\/\">Apex Error Logging Framework Built Step by Step<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Apex Stub API lets a test replace one class with a fake version, so the code under test runs without inserting records, querying&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2501,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[8,96,243],"tags":[68,98,111,81],"swapz_series":[],"class_list":["post-2504","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","category-testing-debugging","tag-advanced","tag-apex","tag-apex-testing","tag-integration"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Apex Stub API for Mocking Apex Unit Tests<\/title>\n<meta name=\"description\" content=\"The Apex Stub API lets a test replace a class with a fake, so no records or callouts are needed. Learn the StubProvider interface and the rules that apply.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apex Stub API for Mocking Apex Unit Tests\" \/>\n<meta property=\"og:description\" content=\"The Apex Stub API lets a test replace a class with a fake, so no records or callouts are needed. Learn the StubProvider interface and the rules that apply.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-02T07:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-15T16:30:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-stub-api-mocking\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-stub-api-mocking\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\"},\"headline\":\"Apex Stub API for Mocking in Unit Tests Explained\",\"datePublished\":\"2026-11-02T07:30:00+00:00\",\"dateModified\":\"2026-11-15T16:30:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-stub-api-mocking\\\/\"},\"wordCount\":1894,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-stub-api-mocking\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-stub-api-mocking-featured.png\",\"keywords\":[\"Advanced\",\"Apex\",\"Apex Testing\",\"Integration\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\",\"Testing and Debugging\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-stub-api-mocking\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-stub-api-mocking\\\/\",\"name\":\"Apex Stub API for Mocking Apex Unit Tests\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-stub-api-mocking\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-stub-api-mocking\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-stub-api-mocking-featured.png\",\"datePublished\":\"2026-11-02T07:30:00+00:00\",\"dateModified\":\"2026-11-15T16:30:31+00:00\",\"description\":\"The Apex Stub API lets a test replace a class with a fake, so no records or callouts are needed. Learn the StubProvider interface and the rules that apply.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-stub-api-mocking\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-stub-api-mocking\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-stub-api-mocking\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-stub-api-mocking-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-stub-api-mocking-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Apex Stub API featured image showing a test class swapping a real dependency for a fake\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-stub-api-mocking\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Apex Stub API for Mocking in Unit Tests Explained\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apex Stub API for Mocking Apex Unit Tests","description":"The Apex Stub API lets a test replace a class with a fake, so no records or callouts are needed. Learn the StubProvider interface and the rules that apply.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/","og_locale":"en_US","og_type":"article","og_title":"Apex Stub API for Mocking Apex Unit Tests","og_description":"The Apex Stub API lets a test replace a class with a fake, so no records or callouts are needed. Learn the StubProvider interface and the rules that apply.","og_url":"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-11-02T07:30:00+00:00","article_modified_time":"2026-11-15T16:30:31+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a"},"headline":"Apex Stub API for Mocking in Unit Tests Explained","datePublished":"2026-11-02T07:30:00+00:00","dateModified":"2026-11-15T16:30:31+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/"},"wordCount":1894,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-featured.png","keywords":["Advanced","Apex","Apex Testing","Integration"],"articleSection":["Salesforce","Salesforce Developer","Testing and Debugging"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/","url":"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/","name":"Apex Stub API for Mocking Apex Unit Tests","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-featured.png","datePublished":"2026-11-02T07:30:00+00:00","dateModified":"2026-11-15T16:30:31+00:00","description":"The Apex Stub API lets a test replace a class with a fake, so no records or callouts are needed. Learn the StubProvider interface and the rules that apply.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-stub-api-mocking-featured.png","width":2400,"height":1260,"caption":"Apex Stub API featured image showing a test class swapping a real dependency for a fake"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Apex Stub API for Mocking in Unit Tests Explained"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=2504"}],"version-history":[{"count":2,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2504\/revisions"}],"predecessor-version":[{"id":3403,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2504\/revisions\/3403"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/2501"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=2504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=2504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=2504"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=2504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}