I have wrote batch class for setupaudittrail object. In start method query string is

String query = 'SELECT CreatedDate, CreatedBy.Username, Display, Section, Action, DelegateUser,CreatedById,CreatedBy.Name FROM SetupAuditTrail ORDER BY CreatedDate DESC';

How to cover this setup audit trail in test class?

1 Answer 1


You can test the batch class just as you would test any other batch test class.

The only difference is that you don't have to set up test data upfront. SetupAuditTrail records cannot be created from APEX (the object only supports Query() and Retrieve()) but all the audit trail records in your instance are always visible in test context. You don't even need to specify SeeAllData=true.

So a simple example test class could be this (assuming your batch class name is SetupAuditTrailBatch):

private class TestSetupAuditTrailBatchClass {
   static testmethod void test() {
      SetupAuditTrailBatch c = new SetupAuditTrailBatch();

And depending on the purpose of your code, you should add Test.Assert methods to check that the code functioned as expected.

  • 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.
    – Jana Shree
    Commented Oct 4, 2016 at 10:22
  • following error has been occured
    – Jana Shree
    Commented Oct 4, 2016 at 10:22
  • yeah that's a known limitation of batch class testing. See here for the workaround: help.salesforce.com/apex/… To further help you I'd need to see the full code. Commented Oct 4, 2016 at 12:20

You must log in to answer this question.

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