1

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:

  1. a value is set in PK__c (External ID); and
  2. 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.

4
  • Are you saying your code coverge shows 0% for TA_ClientLink_SetPrimaryKey as well? Or just ClientLinkTrigger? Commented Oct 31, 2022 at 14:25
  • beforeInsert records should. not be mocked with Ids
    – cropredy
    Commented Oct 31, 2022 at 18:12
  • @KrisGoncalves - it was just the ClientLinkTrigger. I added ``` insert(newList); ``` ... and it went through. I am unsure if that was right, but it needed to be a specific insert, whereas I thought the framework meant the specific inserts shouldn't be required.
    – G-Man
    Commented Nov 2, 2022 at 3:45
  • @cropredy - what do you mean they should not be mocked with IDs? Is there a reason you'd suggest this should not be done?
    – G-Man
    Commented Nov 2, 2022 at 3:48

1 Answer 1

3

@G-man, unfortunately you still need a test to cover the trigger itself.

I would recommend using TriggerBase.bypass(String sObjectName) to make sure it is a no-op test.

@IsTest
private static void shouldBeAbleToPerformDml() {
  Exception caught;
  TriggerBase.bypass('ClientLink__c');

  try {
    insert new ClientLink__c();
  } catch (Exception e) {
    caught = e;
  }

  System.assertEquals(
    null,
    caught,
    'There should be no problems when performing DML on a "ClientLink__c" record.'
  );
}
1
  • Thanks Mitch. Absolutely love the framework!
    – G-Man
    Commented Dec 1, 2022 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .