I cannot get my head around code coverage. I have been given the task to add code to avoid recursive calls. This is pretty straight forward but what I cannot understand is now the code coverage has gone from 95% to 8% by adding one line of code. Why have I tanked my code coverage?
trigger SEL_COL_SetIncidentStaffonAction on
BMCServiceDesk__IncidentHistory__c (before insert) {
if(checkRecursive.runOnce()){
Set<Id> incIds = new Set<Id>();
for (BMCServiceDesk__IncidentHistory__c inchist : Trigger.new){
incIds.add(inchist.BMCServiceDesk__FKIncident__c);
}
Map<Id, BMCServiceDesk__Incident__c> incmap = new Map<Id, BMCServiceDesk__Incident__c>(
[
Select Id, BMCServiceDesk__FKOpenBy__c,ownerid,name,SEL_COL_Client_Reference__c from BMCServiceDesk__Incident__c where Id in :incIds
]);
for (BMCServiceDesk__IncidentHistory__c inchist : Trigger.new){
if(incmap!=null)
{
if (incmap.get(inchist.BMCServiceDesk__FKIncident__c).BMCServiceDesk__FKOpenBy__c != null){
inchist.SEL_COL_Incident_Staff__c = incmap.get(inchist.BMCServiceDesk__FKIncident__c).BMCServiceDesk__FKOpenBy__c;
}
//if the incident history is related with atos then we put a flag, so Pentaho knows this is related with ATOS.
if(incmap.get(inchist.BMCServiceDesk__FKIncident__c).SEL_COL_Client_Reference__c!=null){
Datetime dt = Datetime.now();
Datetime createdDT=Datetime.now();
String formattedDate;
String formattedDT;
if(dt!=null)
formattedDate = dt.format('yyyy/MM/dd HH:mm:ss z');
if(createdDT!=null)
formattedDT=createdDT.format('yyyy/MM/dd HH:mm:ss z');
inchist.transactionDate__c=formattedDate;
inchist.createdDateGMT__c=formattedDT;
inchist.FTP_To_Process__c=true;
inchist.SEL_COL_Client_Reference__c=incmap.get(inchist.BMCServiceDesk__FKIncident__c).SEL_COL_Client_Reference__c;
}
}
}
}
Test Class
@isTest
(seealldata=true)
public class SEL_COL_SetIncidentStaffonActionTest {
static testMethod void testIncidentStaff(){
if (checkRecursivetest.runAfterOnce())
{
Profile p = [select Id from Profile where Name = 'ServiceDesk Client'];
User tuser = new User(alias='u2',email='[email protected]',
emailencodingkey='UTF-8', lastname='TestingUser', languagelocalekey='en_US',
localesidkey='en_US',profileid=p.Id, country='United States',
timezonesidkey='Europe/London',username='[email protected]');
insert tuser;
QueueSObject queue = [select QueueId from QueueSObject where SObjectType = 'BMCServiceDesk__Incident__c' limit 1];
BMCServiceDesk__Status__c status = [select Id from BMCServiceDesk__Status__c where Name = 'WORK IN PROGRESS' Limit 1];
BMCServiceDesk__Impact__c impact = [select Id from BMCServiceDesk__Impact__c where Name = 'LOW' Limit 1];
BMCServiceDesk__Urgency__c urgency = [select Id from BMCServiceDesk__Urgency__c where Name = 'LOW' Limit 1];
BMCServiceDesk__BMC_BaseElement__c service = [select Id from BMCServiceDesk__BMC_BaseElement__c where BMCServiceDesk__ClassID__c = 'BMC_BUSINESSSERVICE' Limit 1];
BMCServiceDesk__Category__c category = [select Id from BMCServiceDesk__Category__c where BMCServiceDesk__AvailableForIncidents__c = true and BMCServiceDesk__inactive__c = false Limit 1];
String source = 'Telephone';
String incType = 'Incident';
String opCat = 'Test';
String description = 'Test Incient';
String ref = 'ref';
BMCServiceDesk__Incident__c Tincident = [SELECT Id, Name, COL_SEL_CC_User__c, SEL_COL_Client_Reference__c, SEL_COL_Site__c,
BMCServiceDesk__dueDateTime__c, BMCServiceDesk__FKAccount__c,
BMCServiceDesk__FKContact__c, BMCServiceDesk__FKCategory__c,
BMCServiceDesk__FKPriority__c,BMCServiceDesk__FKBMC_BaseElement__c,
SEL_COL_Site__r.BMCServiceDesk__PostalCode__c, SEL_COL_Site__r.BMCServiceDesk__Address__c,
SEL_COL_Site__r.BMCServiceDesk__Country__c, SEL_COL_Site__r.BMCServiceDesk__City__c,
SEL_COL_Site__r.BMCServiceDesk__StateOrProvince__c, Preferred_Date__c, Preferred_Slot_AM_PM__c
FROM BMCServiceDesk__Incident__c
LIMIT 1];
BMCServiceDesk__Incident__c incident = new BMCServiceDesk__Incident__c(BMCServiceDesk__FKClient__c = tuser.Id,
COL_SEL_CC_User__c = Tincident.COL_SEL_CC_User__c,
SEL_COL_Site__c = Tincident.SEL_COL_Site__c,
BMCServiceDesk__FKAccount__c = Tincident.BMCServiceDesk__FKAccount__c,
BMCServiceDesk__FKContact__c = Tincident.BMCServiceDesk__FKContact__c,
BMCServiceDesk__FKPriority__c = Tincident.BMCServiceDesk__FKPriority__c,
BMCServiceDesk__FKBMC_BaseElement__c = Tincident.BMCServiceDesk__FKBMC_BaseElement__c,
BMCServiceDesk__contactType__c = source, BMCServiceDesk__IncidentType__c = incType,
BMCServiceDesk__FKBusinessService__c = service.Id, BMCServiceDesk__FKCategory__c = category.Id,
SEL_COL_Client_Reference__c = ref, OwnerID = queue.QueueId);
// Test.startTest();
insert incident;
// Test.stopTest();
BMCServiceDesk__Action__c action = [Select Id from BMCServiceDesk__Action__c where BMCServiceDesk__appliesToIncident__c = True and BMCServiceDesk__inactive__c = False limit 1];
BMCServiceDesk__IncidentHistory__c inchist = new BMCServiceDesk__IncidentHistory__c();
inchist.BMCServiceDesk__note__c = 'test note';
inchist.BMCServiceDesk__FKAction__c = action.Id;
inchist.BMCServiceDesk__FKIncident__c = incident.Id;
Test.startTest();
insert inchist;
Test.stopTest();
// confirm test
BMCServiceDesk__IncidentHistory__c incidenthist = [select Id, BMCServiceDesk__FKIncident__c
from BMCServiceDesk__IncidentHistory__c
where BMCServiceDesk__FKIncident__c = :incident.Id limit 1];
system.assert(incident.Id == incidenthist.BMCServiceDesk__FKIncident__c);
}
}
}
CheckRecursive Class
public Class checkRecursive{
Private static boolean run = true;
Public static boolean runOnce()
{
if(run)
{
run=false;
return true;
}
else
{
return run;
}
}
}
@isTest(seeAllData=true)
is a red flag. While unlikely to be the cause of your issues, it's still worth mentioning. Best practice is to create the data you need for your test inside the test class itself. Otherwise, you open yourself up to failures due to pre-existing data in one environment that does not exist in the environment you're testing in.