I have a trigger on Opportunity which runs when the Opportunity is closed as Won.
The trigger checks if the Opportunity line item has a specific product(Id is hard coded in the code) and then does sequence of events.
I am trying to increase the test coverage for this class but the problem I am facing is I create a test product and simulate all the actions but the if block in trigger where Id is compared always evaluate to false as the test product Id does not match with the Id hard coded in the trigger. In this case how to increase the test coverage?
One option i see is to enable the seeAllData=TRUE
on the test class and use the real product but I would like to know if there is any better option in this scenario?
EDIT:
Below is the code that I am trying to unit test:
trigger UpdateTRACKCaseDates on Case (after update) {
//This is to get the Record Type ID for the child Case.
Id childCaseRTId = Case.sObjectType.getDescribe().getRecordTypeInfosByName().get('Child Case').getRecordTypeId();
Set<Id> oppIds = new Set<Id>();
Map<String,Case> psCaseObjMap = new Map<String,Case>(); //This collection holds the oppID and the corresponding TEST_PRODUCT case Object.
for(Integer i =0; i< Trigger.size; i++){
if(Trigger.isUpdate){
Boolean condition = Trigger.new[i].IsClosed;
condition = condition && (Trigger.new[i].Product__c == '01tdZZZZZZZZzyFguAAE'); // Id 01td0000000yFguAAE is for TEST_PRODUCT product
condition = condition && (Trigger.old[i].Campaign_End_Date__c != Trigger.new[i].Campaign_End_Date__c);
if(condition){
Id oppId = Trigger.new[i].Opportunity__c;
psCaseObjMap.put(String.valueOf(oppId),(Case)(Trigger.new[i]));
oppIds.add(oppId);
}
}
}
List<Sobject> oppSObList = [SELECT Id, (SELECT CaseNumber,Description,Subject,Product__c FROM Cases__r WHERE Product__c = '01tdKKKKKKKKKyFgpAAE') FROM Opportunity WHERE Id IN :oppIds];
List<Case> ptCaseObjList = new List<Case>();
for(Sobject oppSobj: oppSObList){
Case ptCaseObj = (Case)oppSobj.getSObjects('Cases__r')[0];
ptCaseObj.Campaign_Start_Date__c = psCaseObjMap.get(String.valueOf(oppSobj.get('Id'))).Campaign_Start_Date__c;
ptCaseObj.Campaign_End_Date__c = psCaseObjMap.get(String.valueOf(oppSobj.get('Id'))).Campaign_End_Date__c;
ptCaseObjList.add(ptCaseObj);
}
upsert ptCaseObjList;
}