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
Cameron SeitzCameron Seitz 

Test Classes for Batch Apex

Can't seem to figure out how to get code coverage for the Apex Batch Class I have below. 
global class placementTimecardAuditFlag implements Database.Batchable<sObject>, Database.Stateful {

  List<jstcl__TG_Timesheet__c> query = [SELECT jstcl__Placement__r.Name FROM jstcl__TG_Timesheet__c WHERE jstcl__Placement__r.ts2__Status__c ='Active' AND jstcl__Placement__r.jstcl__Timesheet_Period__c != 'Weekly Split' AND jstcl__Week_Ending__c = LAST_N_DAYS:15 AND jstcl__Status__c = 'Pending'];
    Set<String> encounteredNames = new Set<String>();
    Set<String> duplicateNames = new Set<String>();

global Database.QueryLocator start(Database.BatchableContext bc) {
        for(jstcl__TG_Timesheet__c item : query){
    if(encounteredNames.contains(item.jstcl__Placement__r.Name)){
        duplicateNames.add(item.jstcl__Placement__r.Name);
    }
    else{encounteredNames.add(item.jstcl__Placement__r.Name);}
    }
      return Database.getQueryLocator('SELECT Id,Checkbox1__c FROM jstcl__TG_Timesheet__c WHERE jstcl__Placement__r.Name IN :duplicateNames');}
      

 global void execute(Database.BatchableContext BC, List<jstcl__TG_Timesheet__c> a){
    for(jstcl__TG_Timesheet__c b : a) {
        if(b.Checkbox1__c = True){
           b.Checkbox1__c = False;
        }
        else{b.Checkbox1__c = True;}}
update a;        
    }
global void finish(Database.BatchableContext BC){}
}
This is what I have so far for a test class. Basically what I need it to do is to check that when the test records are inserted that the TCEDAudit__c field on the ts2__Placement__c record is getting set to 1. 
 
@istest
public class placementTimecardAuditFlagTest{
    
    @testSetup static void setupTimesheet() {
// Create common test timesheets with a week ending date that exceeds the previous 15 days
        List<jstcl__TG_Timesheet__c> testTimesheets = new List<jstcl__TG_Timesheet__c>();
        for(Integer i=0 ;i <200;i++) {
            testTimesheets.add(new jstcl__TG_Timesheet__c(jstcl__Week_Ending__c = date.newInstance(2000, 12, 1)));
        }
        insert testTimesheets;       

// Create common test placements containing Active status
        List<ts2__Placement__c> testPlacements = new List<ts2__Placement__c>();
        for(Integer i=0 ;i <200;i++) {
            testPlacements.add(new ts2__Placement__c(ts2__Status__c = 'Active'));

        }
        insert testPlacements;        
//Begin testing
		Test.startTest();  
        
    	placementTimecardAuditFlag obj = new placementTimecardAuditFlag();
        DataBase.executeBatch(obj); 
        
        Test.stopTest();
        System.AssertEquals(testPlacements.TCEDaudit__c,1);
    }}

 
Raj VakatiRaj Vakati
Data setup is not correct .. use this code
 
@istest
public class placementTimecardAuditFlagTest{
    
    @testSetup static void setupTimesheet() {
// Create common test timesheets with a week ending date that exceeds the previous 15 days
        List<jstcl__TG_Timesheet__c> testTimesheets = new List<jstcl__TG_Timesheet__c>();
		jstcl__Placement__c placement = new jstcl__Placement__c(); 
		placement.Name='Test' ;
		placement.jstcl__Timesheet_Period__c ='Monthly' ;
		insert placement;
		
        for(Integer i=0 ;i <200;i++) {
            testTimesheets.add(new jstcl__TG_Timesheet__c(jstcl__Week_Ending__c = date.newInstance(2000, 12, 1) ,
			jstcl__Week_Ending__c =System.today().adddays(-20) , jstcl__Status__c ='Pending' ,jstcl__Placement__c = placemnet.Id ,
			));
        }
        insert testTimesheets;       

// Create common test placements containing Active status
        List<ts2__Placement__c> testPlacements = new List<ts2__Placement__c>();
        for(Integer i=0 ;i <200;i++) {
            testPlacements.add(new ts2__Placement__c(ts2__Status__c = 'Active'));

        }
        insert testPlacements;        
//Begin testing
		Test.startTest();  
        
    	placementTimecardAuditFlag obj = new placementTimecardAuditFlag();
        DataBase.executeBatch(obj); 
        
        Test.stopTest();
        System.AssertEquals(testPlacements.TCEDaudit__c,1);
    }}