I have an CI system using Jenkins and the metadata API to automatically execute all unit tests in our packaging org. Some, but not all, of the unit tests have started failing at the point where the test methods make a call to system.test.startTest(). The error is

"System.FinalException: Method only allowed during testing stack (System Code)"

Seems pretty nonsensical since the context is most definitely a unit test class and test method. The same unit tests that fail when executed via metadata API are executing/passing without issue via dev console and standard web UI.

Thoughts anyone?

  • 1
    We are having a similar issue. We have 5 developer orgs where all tests run fine. Then we have a packaging org where the tests run fine. Then when we create the package we get failing tests with the error message: "System.FinalException: Method only allowed during testing stack (System Code)" On around 50 of our 400+ tests. This didn't happen before today. We have made the package many times but before the latest update we did not see this message before. Tried varun guruvugari's idea but running the tests in that way produces no errors.
    – user13072
    Commented Nov 12, 2014 at 14:51
  • 1
    After a bit more investigation it seems the problem starts occurring only after executing a unit test which 'enqueues' a Queuable job. After that test completes, all other unit tests which call 'system.test.startTest()' will begin to fail with the above error. Something about 'enqueuing' a job in one test is affecting the execution state of the remaining unit tests. I think I'll need to open a case with Salesforce now since this seems like a platform issue. Commented Nov 13, 2014 at 23:26
  • @IanMacDonald It's actually a red herring, per se. I found that there's an underlying error "Attempted to rollback to the wrong limit tracker" that appears on a failed DML in these test classes, when System.enqueueJob is used and at least two test methods are present.
    – sfdcfox
    Commented Dec 16, 2014 at 4:59

2 Answers 2


There is a Known Issue where if you use System.enqueueJob in code anywhere, and there are at least two test methods in the same class, it can fail with that error, a "Attempted to rollback to the wrong limit tracker" error, or an Internal Server Error (depending on the cause). A DML that fails in this class will fail with "Attempted to rollback to the wrong limit tracker" when "allOrNone" is "true", or Internal Server Error when "allOrNone" is false. The only two workarounds are to (a) not run System.enqueueJob inside a test method (guard with Test.isRunningTest()), or (b) use at most one test method per class that uses a Queueable interface class. Calling Test.startTest() or Test.stopTest() in a test method that is in an affected class will give the System.FinalException error.


Can you Please clear test data and then run the tests. We can run all the tests of the My namespace classes: Go to Apex Test Execution -> Select Tests -> Choose My Namespace from the picklist and then run those classes as well.

  • Thanks for the response Varun but clearing the test data didn't seem to have any effect on my failing tests. The same tests continue to fail -- but again only fail when run using the metadata API. Commented Nov 13, 2014 at 8:53

You must log in to answer this question.

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