+ Start a Discussion
sfdc dev 2264sfdc dev 2264 

Batch class execute method not calling in test class

Hi,

I have an execute method which isnot covering in my test class , let me know how to cover it

I am having 3 method under execute method which i am calling seperately


MY BATCH CLASS EXECUTE METHOD

  // The execute method will call three methods
    global void execute(Database.BatchableContext bc, List<Contract__c> scope) {
        ContractfieldsupdateonAccount(scope);
        contractcommencementdate(scope);
        contractexpirydate(scope);

    }

MY TEST CLASS :

 Test.startTest();
                list<Contract__c> Cons = new list<Contract__c>();
     
                 Set<id> contractId = new Set<id>();
                 for(Contract__c c :Cons )
                 {
                    contractId.add(c.id);
                 }
            ContractBatchTriggerHandler batchObj = new ContractBatchTriggerHandler(contractId);
              List<contract__c> co=[SELECT Status__c, Account__c FROM Contract__c WHERE Status__c = 'Signed by Customer'];
              
              ContractBatchTriggerHandler.contractcommencementdate(co);
              ContractBatchTriggerHandler.contractexpirydate(co);
              ContractBatchTriggerHandler.ContractfieldsupdateonAccount(co);
              
                  
            Database.executeBatch(batchObj,2);
            Test.stopTest();

Kindly help me how to fix this issue

Thanks
Felix van Hove 9Felix van Hove 9
From your code it would appear you initialize your batch ContractBatchTriggerHandler with an empty Set. My best guess is that the execute method is never called due to a lack of records to process. What about a System.debug() in your start method that verifies you actually have records to process? Do you somewhere create Contract__c items?

I'd also recommend to separate testing of the 3 static ContractBatchTriggerHandler methods from testing the execute method; to add some asserts ; and to move Test.startTest() down in your code before you actually start testing.
ApuroopApuroop
After the SOQL query for Contract__c object, add this line
System.assertNotEquals(0, co.size());

You should have atleast 1 record to process in order to hit the execute method. The above statement will fail as long as you don't have anything in your SOQL. Also, when you are creating the Contract__c records, make sure you have the Status__c = 'Signed by Customer'.