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
Bablu Kumar PanditBablu Kumar Pandit 

How to Cover Execute Method In test class

Hii Every One My Test Class cover 75% covrrage But not coveer Execete Method Please help me
// Handler Class
public class PardotIdsSyncBatch implements Schedulable,Database.Batchable<sObject>,Database.Stateful
{
    String sObjectName = '' ;
    public PardotIdsSyncBatch(String sObjectName)
    {
        this.sObjectName = sObjectName ;
    }
    public void execute(SchedulableContext sc)
    {
        Database.executeBatch(new PardotIdsSyncBatch(sObjectName)) ;
    }
    public Database.QueryLocator start(Database.BatchableContext bc)
    {
        String query = 'select Id,pi__url__c,Pardot_Id__c  from '  + sObjectName + ' where pi__url__c != null AND Pardot_Id__c = null' ;
        return Database.getQueryLocator(query) ;
    }
    public void execute(Database.BatchableContext bc, List<sObject> scope)
    {
        PardotIdsSync sync1 = new PardotIdsSync() ;
        sync1.isAsync = true ;
        sync1.sync(scope) ;     
    }
    public void finish(Database.BatchableContext bc)
    {
        if(this.sObjectName == 'Contact')
            Database.executeBatch(new PardotIdsSyncBatch('Lead') ) ;
        else
            Database.executeBatch(new ActivitySyncBatch(0,null,0));        
    }
}

<========Test Classs=========>
@isTest
public class PardotIdsSyncBatchTest {

    static testMethod void TestData(){
        String CRON_EXP = '0 0 0 15 3 ? *';
        List<contact> lstcon = New List<Contact>(); 
        String sobjectname ='contact';
        Account objacc = New Account();
        objacc.Name ='Test';
        objacc.Type = 'Traveler';
        insert objacc;
        
        contact objcon = New contact();
        objcon.LastName = 'Data';
        objcon.Type__c = 'Traveler';
        objcon.pi__url__c = 'ww.google.com';
        objcon.Pardot_Id__c = 'F003';
        objcon.AccountId = objacc.Id;
        lstcon.add(objcon);
        
        test.startTest();
        	PardotIdsSyncBatch obj = New PardotIdsSyncBatch(sobjectname);
            Database.executeBatch(obj);
           
            String jobId = System.schedule('ScheduleApexClassTest',  CRON_EXP, obj);
            CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
            System.assertEquals(CRON_EXP, ct.CronExpression);
            System.assertEquals(0, ct.TimesTriggered);

            
        test.stopTest();
    }
}
VinayVinay (Salesforce Developers) 
Hi Bablu,

>> Execute method will be called only once.
>> If you have chained batch classes, create separate methods for each and every batch class.
>> Make sure the start method fetches the required to process in execute method.

Review below examples which can help you.

https://developer.salesforce.com/forums/?id=906F0000000MJbiIAG
https://salesforce.stackexchange.com/questions/57220/test-class-not-covering-the-execute-method    
https://salesforce.stackexchange.com/questions/218157/test-class-isnot-covering-execute-method/218160

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar