function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
keerthana chowdharykeerthana chowdhary 

test class passesdwith 50 % code coverage but excute method variables not covering

public  class AUditingBatch implements Database.Batchable<sObject>,Database.Stateful
{
public Database.querylocator start(Database.BatchableContext BC){

string query1='SELECT Id,Action,DelegateUser,CreatedBy.Name,CreatedDate,Display,Section FROM SetupAuditTrail';
return Database.getQueryLocator(query1);
    }
public void execute(Database.BatchableContext BC, List<SetupAuditTrail> scope )
    {
  List<AUditTrial__c> auditList= new List<AUditTrial__c>();//INTIALZE CUSTOM OBJECT
        for(SetupAuditTrail t : scope){ 
        AUditTrial__c AU= new AUditTrial__c(
                   Action__c = t.Action,
                   Name= t.CreatedBy.Name,
                   CreatedBy__c= t.CreatedBy.Name,
                   CreatedDate__c = t.CreatedDate,
                   DelegateUser__c= t.DelegateUser,
                   Display__c= t.Display,
                   Section__c= t.Section);
               auditList.add(AU);
        }
    insert auditList;
    }
public void finish(Database.BatchableContext BC)
{
    system.debug('capture the setup audit trial Data finished');
  }
}

test class


@isTest 
public class TestAudingbatch {
static Testmethod void  testAU(){ 
string query = 'Select Action__c,Display__c,Section__c, CreatedBy__c, CreatedDate__c, DelegateUser__c From AUditTrial__c';
 AUditTrial__c[] m1=new List<AUditTrial__c>();

for (integer i=0; i<10;i++)
{
AUditTrial__c a1=new AUditTrial__c(
Action__c ='apexclass',
Display__c='modified',
Section__c='pagelayout');
 m1.add(a1);
}
insert m1;
    Database.QueryLocator QL;
    Database.BatchableContext BC;
    List <SetupAuditTrail> setup=new List<SetupAuditTrail>();
    List<AUditTrial__c> Auditt = new List<AUditTrial__c>();
    test.startTest();
    AUditingBatch AU = new AUditingBatch();
    QL = AU.start(bc);
    AU.finish(BC);
   AU.execute(BC,setup);


    test.stopTest();
}
Tarun J.Tarun J.
Hello Keerthana,

Try this:
//Comment below lines in your code
//QL = AU.start(bc);
//AU.finish(BC);
//AU.execute(BC);

//Add following line
Database.executeBatch(AU);

-Thanks,
TK
keerthana chowdharykeerthana chowdhary
ERROR
System.UnexpectedException: No more than one executeBatch can be called from within a testmethod. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation
KunalSharmaKunalSharma
Hi Keerthana

You cannot call the execute method more than once from a Test Class. Please create one more query that will run only from the Test Class. PFB example for same:

public Database.querylocator start(Database.BatchableContext BC){
string query1='SELECT Id,Action,DelegateUser,CreatedBy.Name,CreatedDate,Display,Section FROM SetupAuditTrail';
string query2='SELECT Id,Action,DelegateUser,CreatedBy.Name,CreatedDate,Display,Section FROM SetupAuditTrail LIMIT 5;
return Database.getQueryLocator(query1);
if(System.Test.isRunningTest()){
     return Database.getQueryLocator(query2);
 }
return Database.getQueryLocator(query1);
  }

Here the LIMIT can be anything less than your batch size. In this way when you are running this from a Test Class the execute method will be called only once.