You need to sign in to do that
Don't have an account?

Batch Apex Unit Test: System.UnexpectedException: Error processing messages
Hi,
I've got a batch job that works well in manual testing, processing multiple records in each batch without any issues. However my unit test fails if I set the scope greater than just 1 record with a "System.UnexpectedException: Error processing messages". I'm stumped, does anyone know what this cryptic exception means?
Thanks a lot,
Mike
I appreciate the response, not sure if they're related or not, subtle difference that's Exception, this is UnexpectedException :)
Anyway, here's what I found:
Case #: 07093188
System.UnexpectedException: Error processing messages
Synopsis:
A Batch Apex Job successfully processes multiple records per batch in manual testing. The associated unit test passes only when optional scope parameter is set to 1. If this parameter is set to any number greater than 1, even just 2, it fails with the above exception.
Conclusion:
Approximately 24 hours later, without any changes to any code, this exception is currently not being thrown, and the unit test completes successfully with scope values greater than 2. Thus, at least for now, this issue is resolved. This behavior points to an intermittent SFDC bug of indeterminate frequency. I will be watching for this behavior in the future to confirm.
A Google search yielded a couple of results that support the above conclusion.
In the Twitter post below @accordeon mentions hitting this exception after making no changes to code that was not hitting this exception previously. Based on lack of response its likely the exception went away on its own thus pointing to the intermittent nature of this issue.
https://twitter.com/accordeon/status/119895158379642881
In this SFDC Answers post below the user installed a Force.com Labs package from the AppExchange that included a Batch Apex unit test. In their org it failed with this exception. Clearly this exception was not occurring when the package was uploaded otherwise the upload would have failed. This to points to the intermittent nature of this issue.
http://success.salesforce.com/questionDetail?qId=a1X30000000HYgxEAG
All Answers
Anything useful in this thread? It's the only result that came up in Google so it seems pretty rare.
http://boards.developerforce.com/t5/Apex-Code-Development/Batch-Apex-finish-method-issue/td-p/160736
I appreciate the response, not sure if they're related or not, subtle difference that's Exception, this is UnexpectedException :)
Anyway, here's what I found:
Case #: 07093188
System.UnexpectedException: Error processing messages
Synopsis:
A Batch Apex Job successfully processes multiple records per batch in manual testing. The associated unit test passes only when optional scope parameter is set to 1. If this parameter is set to any number greater than 1, even just 2, it fails with the above exception.
Conclusion:
Approximately 24 hours later, without any changes to any code, this exception is currently not being thrown, and the unit test completes successfully with scope values greater than 2. Thus, at least for now, this issue is resolved. This behavior points to an intermittent SFDC bug of indeterminate frequency. I will be watching for this behavior in the future to confirm.
A Google search yielded a couple of results that support the above conclusion.
In the Twitter post below @accordeon mentions hitting this exception after making no changes to code that was not hitting this exception previously. Based on lack of response its likely the exception went away on its own thus pointing to the intermittent nature of this issue.
https://twitter.com/accordeon/status/119895158379642881
In this SFDC Answers post below the user installed a Force.com Labs package from the AppExchange that included a Batch Apex unit test. In their org it failed with this exception. Clearly this exception was not occurring when the package was uploaded otherwise the upload would have failed. This to points to the intermittent nature of this issue.
http://success.salesforce.com/questionDetail?qId=a1X30000000HYgxEAG
I wonder if this was occurring due to the SOQL problems reported by Salesforce on Feb 23 and 24.
http://status.salesforce.com/trust/status/
I was wondering about that myself, but the fact that it was working fine outside of unit testing, and also with unit tests in which scope was set to 1, led me to believe this probably wasn't the case, but really, who knows... I'll update this thread if/when it happens again, or I find out anything new. Thanks for your help, I appreciate it.
I'm running into this right now. Anyone else experiencing this? We didn't change any code surrounding this area when this started happening.
If I remember correctly, the culprit was modifying the list of sObjects passed to the execute method. It's perfectly OK to modify the sObjects themselves, but if you add/remove sObjects from that list, it doesn't like it at all. The easy workaround is to simply .clone() (shallow is just fine) that passed in list and perform any changes to the quantity/position of the sObjects in the list on the cloned copy of the list and leave the passed in list alone.
I'm running into the same issue. in a Spring 13 sandbox
I've seen this happening under this scenario :
- The batch class is a subclass extending a class that implements Database.Batchable<sObject>.
Despite the frameworks alerts you , that you cannot have classes implementing Database.Batchable<sObject> as subclasses, you are still allowed to do something like this :
abstract class aXX implementing Database.Batchable<sObject>
class primary() {
class secondary() extends aXX {
}
}
BUT! when you batch primary.secondary() to run as a batch, it fails during runtime wither saying :
- System.UnexpectedException: Error processing messages (when testing)
or
- Server error when running a schedulld process
I've seen this happening under this scenario :
- The batch class is a subclass extending a class that implements Database.Batchable<sObject>.
Despite the frameworks alerts you , that you cannot have classes implementing Database.Batchable<sObject> as subclasses, you are still allowed to do something like this :
abstract class aXX implementing Database.Batchable<sObject>
class primary() {
class secondary() extends aXX {
}
}
BUT! when you batch primary.secondary() to run as a batch, it fails during runtime wither saying :
- System.UnexpectedException: Error processing messages (when testing)
or
- Server error when running a schedulld process
public abstract class Batch implements Database.Batchable<Job>, Database.Stateful, Database.AllowsCallouts{
...
}
In my Test Class, I created a subclass which extends Batch. Then in my Test Method, I can call Database.executeBatch(new TestBatch()) to test the Batchable methods of Batch.
@isTest
public class Batch_test {
...
public class TestBatch extends Batch {
...
}
@isTest
public static void testBatchableMethods(){
...
Test.startTest();
TestBatch testBatch = new TestBatch();
...
Database.executeBatch(testBatch);
Test.stopTest(); // <------------------ Where "System.UnexpectedException: Error processing messages" points to
}
}
The solved the issue by placing TestBatch into its own class instead of as a subclass of Batch_test.