I am trying to use the Apex Trigger Actions Framework (https://github.com/mitchspano/apex-trigger-actions-framework) to keep things organised.
I understand all the concepts, but I am having trouble getting a test class to work.
I am just trying to ensure that before a record is inserted into my ClientLink__c object:
- a value is set in PK__c (External ID); and
- that Business__c has a value.
trigger ClientLinkTrigger on ClientLink__c (
before insert,
after insert,
before update,
after update,
before delete,
after delete,
after undelete
) {
new MetadataTriggerHandler().run();
}
public class TA_ClientLink_SetPrimaryKey implements TriggerAction.BeforeInsert {
public void beforeInsert(List<ClientLink__c> newList){
for (ClientLink__c record : newList) {
if(record.Business__c == null) {
record.addError('Client Link cannot be created without specifying Business.');
}
if (record.PK__c == null) {
record.PK__c = string.valueOf(GuidUtil.NewGuid()).toLowerCase(); // note that this works in sandbox - this is not the issue.
}
}
}
}
@isTest
private class TA_ClientLink_SetPrimaryKey_Test {
@isTest
private static void beforeInsertTest() {
List<ClientLink__c> newList = new List<ClientLink__c>();
//generate fake Id
Id myRecordId = TestUtility.getFakeId(ClientLink__c.SObjectType);
newList.add(new ClientLink__c(Id = myRecordId,Name = 'Client'));
new TA_ClientLink_SetPrimaryKey().beforeInsert(newList);
//Use getErrors() SObject method to get errors from addError without performing DML
System.assertEquals(true,newList[0].hasErrors(),'The record should have errors');
System.assertEquals(1,newList[0].getErrors().size(),'There should be exactly one error');
System.assertEquals(newList[0].getErrors()[0].getMessage(),
'Client Link cannot be created without specifying Business.',
'The error should be the one we are expecting'
);
}
}
The code above has no errors, but when I run the tests, it tells me that I have 0% coverage and that the below link in the trigger isn't being called and is flashing at me in red on the trigger when I go to check the code coverage.
new MetadataTriggerHandler().run();
I've followed it through here and tried multiple different things, but I am a bit lost as to whether I have missed something stupid.
The trigger works in a sandbox and updates the field correctly, it is just the deployment and code coverage I am having issues with.
Any help to set me on the right path would be greatly appreciated.
TA_ClientLink_SetPrimaryKey
as well? Or justClientLinkTrigger
?