I have a apex class that calls the API from a managed package (Zuora Order Builder) and these API calls' implementation (within managed package) have web service callouts to an external system (Zuora). To write a test class for my apex class, if I just call my class methods which in turn call the managed API, it will give error that test class can not have callouts. Ideally, I would like to be able to mock the managed API calls I use, similar to mocking http and webservice. I wonder if this is possible in SF and how it can be done.

  • If the managed packaged has a mock impl you should be able to use that. (Although SF says thats what you should do, I have not got it to work yet when I tried it). Otherwise you have to avoid doing anything that would cause a callout int he managed package. In my case I just created the resultant records.
    – Eric
    Jun 29, 2016 at 18:25
  • I wonder if I can mock any class as long as the target class implements an interface, then I can use Test.setMock(target class, instance). Anyone has tried it ? Jun 29, 2016 at 22:20

1 Answer 1


There is a pilot in the Winter'17 Release Notes;

Build a Mocking Framework with the Apex Stub API (Pilot)

Apex now provides a stub API for implementing your own mocking framework. You can define the behavior of stub objects, which are created at runtime as anonymous subclasses of Apex classes. The stub API is composed of the System.StubProvider interface and the System.Test.createStub() method.

You could try and get on the pilot program via your Account Executive. Otherwise you will need to wait for it to become either a public beta or generally available.

  • Immensely useful. Time to call our AE.
    – sfdcfox
    Aug 22, 2016 at 3:20
  • So after reading this: jessealtman.com/2014/06/real-unit-tests-using-apexmocks can you explain why the class not failing when someone changes code downstream is a good thing? Part of unit testing is validating that business requirements are being met right? My initial impression is Stubbing sounds like a fancy way of doing a unit test without asserts as you are manufacturing the outcome? I see where it could fit in but I also see where it could cause all sorts of issues at a business level....Just thinking out loud.
    – Eric
    Aug 22, 2016 at 4:23
  • P.S not sure this helps with the HTTP mock issue though. Every time I attempt to use a Managed Packages Mock Impl outside of the package I still get callouts not supported in unit tests...No matter what I do I cannot get a unit test to work when a managed package makes a callout as a result of inserting an object (say Account or Opp) - I have not tried in a month or so, maybe it works now??
    – Eric
    Aug 22, 2016 at 4:24
  • aargh -- just after I adopted the fflib framework with its provision via interfaces and factories for mocking domain, selector, and service layers comes yet another approach
    – cropredy
    Aug 22, 2016 at 5:07
  • Kind of Disagree with Eric. There is a difference between a unit test to a system integration test. What a mock API enables the developer, is to test every code unit independently. The new API is a good and expected new for apex developers, who try to follow the Enterprise Patterns. :)
    – Marsson
    Jan 23, 2017 at 20:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .