I've encountered an odd situation where the presence or absence of a try/catch block results in an unusual system error:

System.UnexpectedException: Error processing messages

The test case is exercising a negative case in a fairly complex trigger. It's looking for a DmlException to be thrown with a specific error message. The structure of the test looks like this:

public static void runTest() {
    User u = MyFactory.createUser();

    System.runAs(u) {
        Account a = new Account(Name = 'Test Customer'); // and so on - more test setup with simple DML.

        Boolean caught = false;

        try {
            // This is a breaking change that should cause a DmlException.
            oc.Shipment_Date__c = Date.today() - 4;
            // The update trigger does quite a bit of logic 
            // and calls a global method within Accounting Seed managed package.
            update oc;
        } catch (DmlException e) {
            System.assert(e.getMessage().contains(EXPECTED_MESSAGE), 'correct exception message');
            caught = true;

        System.assert(caught, 'caught the right exception');

I've validated, simply by commenting out the try/catch lines, that the code under test behaves correctly - it throws the expected DmlException with the expected message, and does not throw UnexpectedException.

However, if and only if this exception is caught (allowing execution of the test method to continue/complete), I obtain the above System.UnexpectedException, whose stack trace shows

Class.System.Test.stopTest: line 19, column 1

Class.MyClass_Test.runTest: line 752, column 1

Line 752 is Test.stopTest().

Stuff I've Tried

  • If I remove startTest()/stopTest(), the error occurs after execution leaves the test method and at that point shows a gack:

    Internal Salesforce Error: 410301017-156318 (401142610) (401142610)

    This marks the unit test as failed and does show the correct DmlException failure message in the Developer Console's log view (albeit not in the test run view).

  • If I force the assertion inside the catch block to fail, the unit test simply fails with that AssertException, as it should.

  • I've also tried, due to the reference to "messages", hard-coding the exception message that ultimately leads to the DmlException to a short, constant string. This has no effect.

  • Changing the position of Test.startTest() and Test.stopTest() has no effect, unless they're placed inside the try block, in which case a different gack occurs:

    Internal Salesforce Error: 1471456781-129528 (401142610) (401142610)

  • Removing all lines of code save the update statement from the try block has no effect.

  • Updating trigger, handler, and unit test to API v.43 has no effect.

  • Catching a generic Exception rather than DmlException has no effect.

As a result, even though my code works, I cannot technically get the unit test to pass either way.

All references to this error I've located have to do with either Platform Events or Batch Apex. Platform Events are not in use. I don't believe any Batch Apex is being enqueued by the managed package I'm interacting with (Accounting Seed), although I'm not 100% certain of this.

I have a number of other tests that follow the same pattern and interact in similar, if not identical, ways with this managed package, all of which work correctly. The positive case for this functionality also works correctly and passes.

  • No future methods being fired as well? Sep 3, 2018 at 23:14
  • Not that I'm aware of. None in my code, I should say, can't speak for Accounting Seed.
    – David Reed
    Sep 3, 2018 at 23:15
  • what about (workaround) catch (Exception e) ... ? But, hopefully you have premier support and they'll tell you what the gack means
    – cropredy
    Sep 4, 2018 at 4:11
  • @cropredy catching the generic exception also has no effect on the observed issue.
    – David Reed
    Sep 4, 2018 at 4:18
  • 1
    From the error it seems that the stop test is causing an async operation to complete which may be in the managed package. But the lack of any reference to said package makes me wonder. Any chance you can test without AS installed?
    – Eric
    Sep 4, 2018 at 7:20


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.