I have written a bactch class which will delete old records, expect New 1000 records code shown below.
global class OldDataDeleterinAuditTrail implements DataBase.Batchable<sObject>{
AuditTrail__c AuditRecords = [SELECT Id,CreatedDate FROM AuditTrail__c ORDER BY CreatedDate DESC LIMIT 1 OFFSET 1000];
global DataBase.QueryLocator start(DataBase.BatchableContext bc)
{
return DataBase.getQueryLocator([SELECT Id,CreatedDate FROM AuditTrail__c WHERE CreatedDate <= :AuditRecords.CreatedDate]);
}
global void execute(DataBase.BatchableContext bc,List<AuditTrail__c> ListAT)
{
List<AuditTrail__c> ListAudit = new List<AuditTrail__c>();
for(AuditTrail__c at : ListAT)
{
if(at.CreatedDate !=null)
{
ListAudit.add(at);
system.debug('-->>'+ListAudit);
}
}
if(ListAudit.size()>0)
{
delete ListAudit;
}
}
global void finish(DataBase.BatchableContext bc)
{
}
}
and my test class shown below:
@isTest
public class OldDataDeleterinAuditTrailTest
{
Static TestMethod void olddatamethod()
{
AuditTrail__c a = New AuditTrail__c();
a.Action__c = 'Add';
a.CObjectName__c = 'Object Name';
a.Description__c = 'will pass some query here';
a.Status__c = 'Success';
insert a;
delete a;
Test.StartTest();
OldDataDeleterinAuditTrail AuditRecords = new OldDataDeleterinAuditTrail();
Database.executeBatch(AuditRecords);
Test.StopTest();
}
}
Test class error is:
System.QueryException: List has no rows for assignment to SObject Class.OldDataDeleterinAuditTrail.: line 4, column 1 Class.OldDataDeleterinAuditTrailTest.olddatamethod: line 15, column 1
if use Seealldata=True or false i getting following error: 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.
Help me if any have idea on this.
Thanks