You need to sign in to do that
Don't have an account?
Rick Haag
Test Class for Trigger with IF using Metadata
Hello,
I am attempting to write a test class for a trigger where I am leveraging Custom Metadata to Activate/Deactivate the trigger. I am not able to generate code coverage for both scenarios as I cannot insert test data for Custom Metadata.
In the past I used Custom Settings for trigger activation/deactivation and that it was simple enough to insert to test the "if" and the "other". I understand Custom Metadata is the future, so here I am with incomplete code coverage.
Any recommendations on how to do this is appreciated. I have posted the test class below and added the bit for Custom Metadata, but as mentioned it isn't valid, but added to provide a sense of what I am attempting to do.
Trigger
Test Class
I am attempting to write a test class for a trigger where I am leveraging Custom Metadata to Activate/Deactivate the trigger. I am not able to generate code coverage for both scenarios as I cannot insert test data for Custom Metadata.
In the past I used Custom Settings for trigger activation/deactivation and that it was simple enough to insert to test the "if" and the "other". I understand Custom Metadata is the future, so here I am with incomplete code coverage.
Any recommendations on how to do this is appreciated. I have posted the test class below and added the bit for Custom Metadata, but as mentioned it isn't valid, but added to provide a sense of what I am attempting to do.
Trigger
trigger OpportunityContactRole on OpportunityContactRole (after insert, after update, after delete) { //Checks if Custom Metadata on for Trigger is Active List<Trigger__mdt> metadataList = new List<Trigger__mdt>(); metadataList = [SELECT Id FROM Trigger__mdt WHERE isActive__c != NULL AND Label = 'OpportunityContactRole']; if(metadataList.size()==0) { return; } for(OpportunityContactRole ocr : Trigger.new==null ? Trigger.old : Trigger.new) { ------Run Stuff Here---- } }
Test Class
@isTest private class OpportunityContactRoleTest { @isTest static void triggerOn() { Account acc = new Account(name='test acc'); insert acc; Contact con = new Contact(lastname='test con',accountid=acc.id); insert con; Opportunity opp = new Opportunity(AccountId=acc.id,Amount=1,Name = 'test opp',StageName = 'Closed Lost',CloseDate=Date.today()); insert opp; //This is will work for Custom Settings but not for Custom Metadata Trigger_mdt setting = new TriggerManager__mdt(); setting.Label = 'OpportunityContactRole'; setting.isActive__c = TRUE; insert setting; //Insert OpportunityContactRole OpportunityContactRole ocr = new OpportunityContactRole(); ocr.ContactId = con.Id; ocr.OpportunityId = opp.Id; ocr.IsPrimary = TRUE; ocr.Role = 'Decision Maker'; test.starttest(); insert ocr; test.stoptest(); } @isTest static void triggerOff() { Account acc = new Account(name='test acc'); insert acc; Contact con = new Contact(lastname='test con',accountid=acc.id); insert con; Opportunity opp = new Opportunity(AccountId=acc.id,Amount=1,Name = 'test opp',StageName = 'Closed Lost',CloseDate=Date.today()); insert opp; //Insert OpportunityContactRole OpportunityContactRole ocr = new OpportunityContactRole(); ocr.ContactId = con.Id; ocr.OpportunityId = opp.Id; ocr.IsPrimary = TRUE; ocr.Role = 'Decision Maker'; test.starttest(); insert ocr; test.stoptest(); } }
You don't need to insert Custom metadata records in the test class, the system fetches the metadata records, unlike the custom settings.
In my trigger, there is one scenario where the metadata field is "not NULL". This gets the code coverage 14 of 15
I am not able to test "if" part of the trigger. How do I test the "NULL", (if), scenerio when the actual setting of the Custom Metadata in the in the "System" is "Not NULL".
Scenerio #1 in Test Class
"metadataList = [SELECT Id FROM Trigger__mdt WHERE isActive__c != NULL AND Label = 'OpportunityContactRole']"
This works because isActive = TRUE (code coverage 14/15)
Scenerio #2 in Test Class
metadataList = [SELECT Id FROM Trigger__mdt WHERE isActive__c = NULL AND Label = 'OpportunityContactRole']
This scenario doesn't exist in the test class because Scenerio #1 persists as it is in the Custom Metadata. Therefore no Code Coverage.
Not sure how to account for "Scenario #2" in the Test Class so I get coverage for Line 10 in the trigger and thus 15/15 code coverage.
If you want to use custom metadata and test all the scenarios, then try creating two fields isActive1__c and isActive2__c and modify the logic in apex class.