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
JoshTonksJoshTonks 

Need help getting coverage on a Test Class for a Batch

Im writing a batch class I have written some previously without issue however when i am writing this this and only 42% coverage. What im trying to do check a box on records that hit the contract end date today which runs a process. It all works just cannot figure out what i need to do get it to test successfully.

This is my Class
 
global class DDBatchUpdate implements Database.Batchable<sObject> {
    Date TODAY = Date.today();
    String DDACTIVE = 'Active';
    global Database.QueryLocator start(Database.BatchableContext BC) { 
        String query = 'SELECT Id FROM DD_Agreement__c WHERE DD_Status__c =: DDACTIVE AND Rolling_End_Date__c = TODAY';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<DD_Agreement__c> DDList) {
        for(DD_Agreement__c DD : DDList)
        {        
            DD.id = DD.id;
            DD.Trigger_Builder__c = TRUE;
        }
        try {
            
            update DDList;
        } catch(Exception e) {
            System.debug(e);
        }
    }   
    global void finish(Database.BatchableContext BC) {
        
  }
}

This is my test class which is only getting 42% coverage It is nearly all the content in global void execute. It is probably something simple im missing.
 
@isTest
private class DDBatchUpdateTest {

static testmethod void test() {

List<DD_Agreement__c> DDList = new List<DD_Agreement__c>(); 
for (Integer i=0;i<200;i++) { 
    DDList.add(new DD_Agreement__c(Trigger_Builder__c=TRUE,DD_Status__c='Active')); 
}
insert DDList;

Test.startTest();
DDBatchUpdate c = new DDBatchUpdate();
Database.executeBatch(c);
Test.stopTest();
    
DD_Agreement__c[] DDUpdatedList = [SELECT Id,Trigger_Builder__c FROM DD_Agreement__c];
System.assert(DDUpdatedList[0].Id != null); 
}
}

Thank you in advance.

 
Best Answer chosen by JoshTonks
JoshTonksJoshTonks
I managed to fix it I put some variables in in the test class and then included the same critieria that covers the update and the query 
 
isTest
private class DDBatchUpdateTest {

static testmethod void test() {
    Date TODAY = Date.today();
    String DDACTIVE = 'Active';
List<DD_Agreement__c> DDList = new List<DD_Agreement__c>(); 
for (Integer i=0;i<200;i++) { 
    DDList.add(new DD_Agreement__c(Trigger_Builder__c=TRUE,DD_Status__c=DDACTIVE,Rolling_End_Date__c = TODAY)); 
}
insert DDList;

Test.startTest();
DDBatchUpdate c = new DDBatchUpdate();
Database.executeBatch(c);
Test.stopTest();
    
DD_Agreement__c[] DDUpdatedList = [SELECT Id,Trigger_Builder__c FROM DD_Agreement__c];
System.assert(DDUpdatedList[0].Id != null); 
}
}