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
Akshay MhetreAkshay Mhetre 

My First test class for Batch class

public class PL_FIAutoAllocationCounterResetBatch  implements Database.Batchable<sObject> {
    
    public Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'SELECT id, Count__c FROM Sales_To_CPA_Allocation_Rule1__c WHERE Count__c !=0';
        return Database.getQueryLocator(query);

    }
    
    public void execute(Database.BatchableContext BC, List<Sales_To_CPA_Allocation_Rule1__c> scope){
        List<Sales_To_CPA_Allocation_Rule1__c> lstRulesToUpdate = new List<Sales_To_CPA_Allocation_Rule1__c>();
        for(Sales_To_CPA_Allocation_Rule1__c rule : scope){
             rule.Count__c = 0;
            lstRulesToUpdate.add(rule);
        }
        if(lstRulesToUpdate.size() > 0){
        update lstRulesToUpdate;
        }
    }
    
    public void finish(Database.BatchableContext BC){
    }
}

Please help me.

Best Answer chosen by Akshay Mhetre
CharuDuttCharuDutt
Hii Akshay
Try Below Test Class
@isTest
public class PL_FIAutoAllocationCounterResetBatchTest {
@istest
    public Static void UnitTest(){
        Sales_To_CPA_Allocation_Rule1__c scar = new Sales_To_CPA_Allocation_Rule1__c();
        scar.Count__c = 5;
        /*Fill All the Fields Required*/
        insert scar;
         test.startTest();
        
        PL_FIAutoAllocationCounterResetBatch lp = new PL_FIAutoAllocationCounterResetBatch();
        Id batchId = Database.executeBatch(lp);
        Test.stopTest();
    }
}
Please mark It As Best Answer If It Helps Thank You!
 

All Answers

CharuDuttCharuDutt
Hii Akshay
Try Below Test Class
@isTest
public class PL_FIAutoAllocationCounterResetBatchTest {
@istest
    public Static void UnitTest(){
        Sales_To_CPA_Allocation_Rule1__c scar = new Sales_To_CPA_Allocation_Rule1__c();
        scar.Count__c = 5;
        /*Fill All the Fields Required*/
        insert scar;
         test.startTest();
        
        PL_FIAutoAllocationCounterResetBatch lp = new PL_FIAutoAllocationCounterResetBatch();
        Id batchId = Database.executeBatch(lp);
        Test.stopTest();
    }
}
Please mark It As Best Answer If It Helps Thank You!
 
This was selected as the best answer
Akshay MhetreAkshay Mhetre
Thanks charudatta...Great. 100% code coverage.