6

We've recently install a new managed package which apparently kicks off multiple batch calls from an Event trigger.

All of our tests on the event object now fail with the following exception System.UnexpectedException: No more than one executeBatch can be called from within a test method. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.

We don't get any details about what this is since it's under the managed package debug log cloaking technology.

Do we have any other options besides uninstalling the package to get our tests passing? Nothing we can control is kicking off a batch process so it's unclear what we need to do. We would comment out the tests but that puts us below 75% code coverage, and is pretty undesirable given up the ability to test any code on the event object.

1 Answer 1

7

You really have three options, (1) vendor fixes it, (2) you work around it, (3) you uninstall the package.

First, contact your managed package provider. It's their responsibility to fix this, either by providing some sort of bypass mechanism, or not calling batches in unit tests (which may reduce their code coverage, but isn't particularly your problem). Given that they risk losing you as a customer, I'd imagine they'd want to fix the problem.

If working with the vendor isn't an option, or time is of the essence you can abuse Database.rollback to undo their work before getting the exception. Using Database.rollback causes all work, including any emails, scheduled jobs, batches, enqueueJobs, etc to disappear. Hence timing is important. If you do it at the start and end of your test methods chances are you should be able to do all of your assertions. Just make sure to not use the stopTest method as this kicks off the batch classes that were spooled.

Before

static testmethod void yourTest() {
  Test.startTest();
  insert generateEvent();
  Test.stopTest();
  assertExpectedBehavior();
}

After

static testmethod void yourTest() {
  System.SavePoint sp = Database.setSavePoint();
  insert generateEvent();
  assertExpectedBehavior();
  Database.rollback(sp);
}
4
  • genius! the rollback totally worked, phew, we were between a rock and a hard place there. to avoid trashing the existing tests, added a savepoint at the start of each test method, rollback at the end, remove startTest / stopTest calls, presto! thank you thank you thank you Commented Oct 17, 2016 at 16:58
  • we've reached out to the managed package provider, and to salesforce support, but both of those are long lead options, who knows how long it'll take to get a fix and get it released to our clients org :( Commented Oct 17, 2016 at 16:59
  • fyi, by just using the rollbacks at the start and stop of each test method you can still do all your assertions ... since absent the stopTest() calls the batches never get spooled ... Commented Oct 17, 2016 at 16:59
  • I have been hit by the same issue. Thx for the Workaround. In my case, nothing has changed in the org except the version (Winter 17) - is this a new error/behaviour? Commented Oct 28, 2016 at 17:59

You must log in to answer this question.

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