1

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?

2

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):

@isTest
private class TestSetupAuditTrailBatchClass {
   static testmethod void test() {
      Test.startTest();
      SetupAuditTrailBatch c = new SetupAuditTrailBatch();
      Database.executeBatch(c);
      Test.stopTest();
   }
}

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

| improve this answer | |
  • 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 Oct 4 '16 at 10:22
  • following error has been occured – Jana Shree Oct 4 '16 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. – Guy Clairbois Oct 4 '16 at 12:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.