Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
Hello,

I have a batch making reference to an history record and I am struggling to get above 75% in my test class.

Could someone help?

Thanks

I have attached the code coverageUser-added image

Here is the batch

public class BatchHealthcareJourneyLastSyncedDate implements Schedulable, Database.Batchable<sObject>{

public void execute(SchedulableContext sc) {

Id batchProcessId = Database.executeBatch(this);

}

public Database.QueryLocator start(Database.BatchableContext bc) {

Map<Id, HealthCare_Journey_Template__c> hcjTempMap = new Map<Id, HealthCare_Journey_Template__c>(

[SELECT Name, Template_Last_Modified__c,

(SELECT Field, NewValue, OldValue, ParentId FROM Histories WHERE Field = 'Template_Last_Modified__c')

FROM HealthCare_Journey_Template__c]);

Set<Id> hcjTempIds = new Set<Id>();

for(HealthCare_Journey_Template__c hcjTmp : hcjTempMap.values()) {

for(HealthCare_Journey_Template__History hcjhist : hcjTmp.Histories) {

if(hcjhist.Field == 'Template_Last_Modified__c'

&& (hcjhist.OldValue != hcjhist.NewValue) && (hcjhist.ParentId == hcjTmp.Id)) {

hcjTempIds.add(hcjTmp.Id);

}

}

}

System.debug('hcjTempIds ' +hcjTempIds);

return Database.getQueryLocator([SELECT Name, Healthcare_Journey_Template__c, Last_Synced_Date__c FROM HealthCare_Journey__c

WHERE Journey_out_of_date__c = true AND Healthcare_Journey_Template__c IN :hcjTempIds]);

}

public void execute(Database.BatchableContext bc, List<HealthCare_Journey__c> scope) {

System.debug('⌗⌗⌗⌗ UPDATED Scope Size ' + scope.size());

List<HealthCare_Journey__c> hcjToUpdate = new List<HealthCare_Journey__c>();

for(HealthCare_Journey__c hcj : scope) {

hcj.Last_Synced_Date__c = System.now();

hcjToUpdate.add(hcj);

}

if(hcjToUpdate.size() > 0) {

update hcjToUpdate;

}

}

public void finish(Database.BatchableContext info) {

System.debug('Done');

}

}

here is my test class: 

@isTest

public class BatchHealthcareJourneyLastSyncedDateTest {

@testSetup

static void setup() {

Indication__c ind = new Indication__c(Name = 'test Indication');

insert ind;

Account acc = new Account(Name = 'test Account');

insert acc;

Healthcare_Journey_Template__c hcjTemp = new Healthcare_Journey_Template__c(Name = 'testHcjTemplate',

Template_Last_Modified__c = System.now().addDays(-1),

Indication__c = ind.Id);

insert hcjTemp;

Step_Master__c stpMaster = new Step_Master__c(Name = 'test Step Master');

insert stpMaster;

Sub_Step_Master__c subStpMaster = new Sub_Step_Master__c(Name = 'Bilan sanguin',

Step_Master__c = stpMaster.Id);

insert subStpMaster;

Template_Step__c tstep = new Template_Step__c(Name = 'testTemplateStep',

Step_Number__c = 1,

Step_Master__c = stpMaster.Id,

HealthCare_Journey_Template__c = hcjTemp.Id);

insert tstep;

Template_Sub_Step__c tsubstep = new Template_Sub_Step__c(Name = 'sub Step',

Sub_Step_Number__c = 1,

Sub_Step_Master__c = subStpMaster.Id,

Template_Step__c = tstep.Id);

insert tsubstep;

List<Healthcare_Journey__c> hcjList = new List<Healthcare_Journey__c>();

for(Integer i=0; i<200; i++) {

hcjList.add(new Healthcare_Journey__c(Name = 'test journey ' +i, Last_Synced_Date__c = System.now().addDays(-1),

Healthcare_Journey_Template__c = hcjTemp.Id));

}

insert hcjList;

List<Healthcare_Journey__c> hcjToUpdate = new List<Healthcare_Journey__c>();

for(Healthcare_Journey__c hcj : hcjList) {

hcj.Last_Synced_Date__c = System.now();

hcjToUpdate.add(hcj);

}

update hcjToUpdate;

Test.startTest();

BatchHealthcareJourneyLastSyncedDateTest batchClass = new BatchHealthcareJourneyLastSyncedDateTest();

batchClass.processTemplateUpdate(hcjTemp.Id);

HealthCare_Journey_Template__History[] hcjHist = batchClass.retrieveTemplateHistory(hcjTemp.Id);

Test.stopTest();

System.debug('hcjHist History ' +hcjHist);

System.assertEquals(1, hcjHist.size());

}

public void processTemplateUpdate(Id hcjTempId) {

Healthcare_Journey_Template__c hcjTemp = new Healthcare_Journey_Template__c(Id = hcjTempId, Template_Last_Modified__c = System.now());

update hcjTemp;

}

public HealthCare_Journey_Template__History[] retrieveTemplateHistory(Id hcjTempId) {

List<HealthCare_Journey_Template__History> hcjHist;

if(Test.isRunningTest()) {

hcjHist = new List<HealthCare_Journey_Template__History>{}; //OldValue, NewValue not writeable

hcjHist.add(new HealthCare_Journey_Template__History(ParentId = hcjTempId, Field = 'Template_Last_Modified__c'));

} else

hcjHist = [SELECT ParentId, Field, NewValue, OldValue FROM HealthCare_Journey_Template__History

WHERE ParentId = :hcjTempId];

return hcjHist;

}

@isTest static void test() {

Test.startTest();

BatchHealthcareJourneyLastSyncedDate hcj = new BatchHealthcareJourneyLastSyncedDate();

Id batchId = Database.executeBatch(hcj);

Test.stopTest();

}

}

 
1 answer
0/9000