+ Start a Discussion
Cameron SeitzCameron Seitz 

Cannot Get Full Coverage on Batch Apex Test Class

I currently am only able to get 50% coverage on a test class that I am writing for some Batch Apex that I have written. The issue is that there are a number of lookups/relationships between objects when attempting to create the test records. Specifically  I am in a time crunch and the batch class does not edit anything particularly important or detrimental to the record. It only queries some relevant records/stipulations and changes a checkbox field on the jstcl__TG_Timesheet__c object that is there purely for tracking. How would I go about getting full coverage? Whether that's a mock or not I don't mind, I just have to get this pushed to production quickly. Thank you so much in advance. Here is my Batch Apex:
 
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) {
           b.Checkbox1__c = True;
        }
update a;

    }
global void finish(Database.BatchableContext BC){
    
    
}

}

Here is my test that gets 50% and an error on the method that states:
"System.QueryException: Use query() for non-count queries | Class.placementTimecardAuditFlagTest2.TimecardTestMethod: line 15, column 1" 
 
@istest 
class placementTimecardAuditFlagTest {
 
static testmethod void TimecardTestMethod(){
    jstcl__TG_Timesheet__c ts = new jstcl__TG_Timesheet__c();
    ts.Checkbox1__c = True;

Test.startTest();
	placementTimecardAuditFlag obj = new placementTimecardAuditFlag();
    DataBase.executeBatch(obj); 
           
Test.stopTest();
System.AssertEquals(database.countquery('SELECT TCEDaudit__c FROM ts2__Placement__c'),1);
    }
   
}


 
Raj VakatiRaj Vakati
Try below 
 
@istest 
class placementTimecardAuditFlagTest {

static testmethod void TimecardTestMethod(){

jstcl__Placement__c placement = new jstcl__Placement__c(); 
	placement.Name='Test' ;
	placement.ts2__Status__c ='Active'
	placement.jstcl__Timesheet_Period__c ='Monthly' ;
	insert placement;
	
	
		List<jstcl__TG_Timesheet__c> testTimesheets = new List<jstcl__TG_Timesheet__c>();

  for(Integer i=0 ;i <200;i++) {	
jstcl__TG_Timesheet__c ts = new jstcl__TG_Timesheet__c();
ts.Checkbox1__c = True;
ts.jstcl__Week_Ending__c  = System/today()-20 ; 
ts.jstcl__Status__c ='Pending' ;
ts.jstcl__Placement__c = placemnet.Id
testTimesheets.add(ts) ; 
}

insert  testTimesheets ; 

Test.startTest();
placementTimecardAuditFlag obj = new placementTimecardAuditFlag();
DataBase.executeBatch(obj); 
	   
Test.stopTest();
 }

}

 
Raj VakatiRaj Vakati
Use below code 
 
@istest 
class placementTimecardAuditFlagTest {

static testmethod void TimecardTestMethod(){

jstcl__Placement__c placement = new jstcl__Placement__c(); 
	placement.Name='Test' ;
	placement.ts2__Status__c ='Active'
	placement.jstcl__Timesheet_Period__c ='Monthly' ;
	insert placement;
	
	
		List<jstcl__TG_Timesheet__c> testTimesheets = new List<jstcl__TG_Timesheet__c>();

  for(Integer i=0 ;i <200;i++) {	
jstcl__TG_Timesheet__c ts = new jstcl__TG_Timesheet__c();
ts.Checkbox1__c = True;
ts.jstcl__Week_Ending__c  = System/today()-20 ; 
ts.jstcl__Status__c ='Pending' ;
ts.jstcl__Placement__c = placemnet.Id
testTimesheets.add(ts) ; 
}

insert  testTimesheets ; 

Test.startTest();
placementTimecardAuditFlag obj = new placementTimecardAuditFlag();
DataBase.executeBatch(obj); 
	   
Test.stopTest();
 }

}

 
Cameron SeitzCameron Seitz
User-added image
Cameron SeitzCameron Seitz
That did not work
Raj VakatiRaj Vakati
You need to mimic the code .. i am not sure what is your API names 

Replace jstcl__Placement__c   with Placement Object APi NAme 

Add ; at line number 20 please and save it