1) I have a custom object "ObjectA__c" with three custom fields "Start_Date__c" and "End_Date__c" and "Status__c".
2) There is already process on this object which runs every time a record of object (ObjectA__c) is created or updated and it checks the following condition :
If Start_Date__c < TODAAY() AND End_Date__c > TODAY()
and based on the above formula evaluation sets the field "Status__c" to 'Yes' otherwise 'No'
But customer asked me to write a batch job which runs every night and looks at each and every record in ObjectA__c and updates the Status__c field based on the start and end dates..
so I developed a batch and here is the pseudo code:
public class SimpleBatch implements Database.Batchable<sObject>{
public Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT Id,Start_Date__c,End_Date__c,Status__c FROM
ObjectA__c WHERE IsDeleted = FALSE';
query = query + ' AND Start_Date__c!=NULL AND End_Date__c!=NULL';
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC,List<ObjectA__c> scope){
List<ObjectA__c> objsToUpdate = new List<ObjectA__c>();
for(ObjectA__c Obj:scope){
if((Obj.Start_Date__c <= Date.today())
&& (Obj.End_Date__c >=Date.today())){
if(Obj.Status__c != 'Yes'){
Obj.Status__c = 'Yes';
objsToUpdate.add(Obj);
}
}else if((Obj.Start_Date__c > Date.today())
|| (Obj.End_Date__c < Date.today())){
if(Obj.Status__c != 'No'){
Obj.Status__c = 'No';
objsToUpdate.add(Obj);
}
}
}
update objsToUpdate;
}
public void finish(Database.BatchableContext BC){
}
}
Test class:
@isTest
public class SimeplTest {
@isTest
public static void scenario_one(){
ObjectA__c obj = new ObjectA__c();
obj.Start_Date__c = Date.today().addDays(-2);
obj.End_Date__c = Date.today().addDays(5);
insert obj;
Test.startTest();
SimpleBatch batch = new SimpleBatch();
Database.executeBatch(batch);
Test.stopTest();
}
}
When I isnert record in test class process is picking it up and changing the status so my batch does not have any thing to process and this is resulting in below 75% of coverage for my batch class.
Any suggestions on how to handle this?