Today I found out something weird about the way static variables act when used while running a test method and I wanted to know if anyone knows why this is the case.
I have a piece of code that creates a log record once an Incident record is created, and once when its status field value is changed to 'Closed'. In order to avoid redundancy I have a static boolean as a flag for the logic to run only once per context. Now, since incident creation and status change to 'Close' always happen on separate context in real life, this wasn't a problem. However, once I tried to write a test method to test my logic I realized that my static boolean flag doesn't reset even though using both test.startTest()
and System.runAs(User)
to start a new context. Eventually I went with this solution of resetting the flag's value manually. But I'm afraid I'm losing the ability to actually keep track of the flag behavior by doing so.
Here is my test method:
@IsTest
static void setClosingIncidentsStateTrackers(){
//Select Required Records
User incidentClient = [SELECT Id FROM User WHERE Username = '[email protected]' LIMIT 1];
BMCServiceDesk__Category__c category = [SELECT Id FROM BMCServiceDesk__Category__c WHERE Name = 'TestCategory'];
BMCServiceDesk__BMC_BaseElement__c service = [SELECT Id FROM BMCServiceDesk__BMC_BaseElement__c WHERE Name = 'TestService'];
BMCServiceDesk__BMC_BaseElement__c serviceOffering = [SELECT Id FROM BMCServiceDesk__BMC_BaseElement__c WHERE Name = 'TestServiceOffering'];
BMCServiceDesk__Urgency__c highUrgency = [SELECT Id FROM BMCServiceDesk__Urgency__c WHERE Name = 'גבוה'];
BMCServiceDesk__Impact__c siteImpact = [SELECT Id FROM BMCServiceDesk__Impact__c WHERE Name = 'אתר'];
Problem_Source__c problemSource = [SELECT Id FROM Problem_Source__c WHERE Name = 'בעיית הגדרות'];
List<BMCServiceDesk__Incident__c> incidents = new List<BMCServiceDesk__Incident__c>();
for(integer i = 0; i < batchSize; i++){
BMCServiceDesk__Incident__c incident = new BMCServiceDesk__Incident__c(
BMCServiceDesk__FKClient__c = incidentClient.ID,
BMCServiceDesk__FKCategory__c = category.ID,
BMCServiceDesk__FKServiceOffering__c = serviceOffering.ID,
BMCServiceDesk__FKBusinessService__c = service.ID,
BMCServiceDesk__FKStatus__c = awaiting_for_handling
);
incidents.add(incident);
}
insert incidents;
for(BMCServiceDesk__Incident__c incident : incidents){
incident.BMCServiceDesk__FKStatus__c = in_work;
}
update incidents;
test.startTest();
System.runAs(new User(Id = UserInfo.getUserId())){
for(BMCServiceDesk__Incident__c incident : incidents){
incident.BMCServiceDesk__FKStatus__c = handled;
incident.BMCServiceDesk__FKBMC_BaseElement__c = service.ID;
incident.BMCServiceDesk__ClosureCategory__c = 'נפתר';
incident.BMCServiceDesk__incidentResolution__c = 'נפתר';
incident.BMCServiceDesk__state__c = false;
}
System.debug(LoggingLevel.ERROR, '*** /// Closing Incidents /// ***');
//MNR_Incident_TriggerHandler.incidentTrackersWereAlreadyCreatedInThisContext = false; <-- This is the manual reset of the static boolean flag
update incidents;
}
test.stopTest();
List<Incident_State_Tracker__c> ists = [SELECT Id, Incident__c, State__c FROM Incident_State_Tracker__c];
System.debug(LoggingLevel.ERROR, '*** // ' + ists.size() + ' // ***');
}
I'd like to know if you guys encounter this issue and know how to force clean new context inside a test method for resetting static variables.
Thank you very much in advance,
Lior.