0

I have a trigger ActionCadenceTrackerChangeTrigger on ActionCadenceTrackerChangeEvent (after insert), and I'm trying to get its coverage above %71, but I'm not able to insert a tracker so this If statement is satisfied.

            CadencetrackerEvent__e newEvent = new CadencetrackerEvent__e(
                TargetId__c = tracker.TargetId,
                State__c = event.State,
                CompletionDisposition__c = event.CompletionDisposition,
                CadenceId__c = tracker.ActionCadenceId,
                RelatedToId__c = tracker.Id
            );
            eventsToPublish.add(newEvent);
        }
    }
 
    if (!eventsToPublish.isEmpty()) {
        try {
            EventBus.publish(eventsToPublish);
        } catch (Exception e) {
            System.debug('Error publishing events: ' + e.getMessage());
        }

Here's my test class for reference:

@isTest
public class TestActionCadenceTrackerChangeTrigger {
   @isTest
   static void testTriggerHandler() {
       // Create test Contact
       Contact testContact = new Contact(
           FirstName = 'Test',
           LastName = 'Contact',
           MailingStreet = '123 MAIN ST',
           MailingCity = 'ANYTOWN',
           MailingState = 'CA',
           MailingPostalCode = '12345',
           MailingCountry = 'USA'
       );
       insert testContact;

       // Create mock ActionCadenceTracker object
      String jsonObject = '[{ "attributes": { "type": "ActionCadenceTracker", "url": "/services/data/v56.0/sobjects/ActionCadenceTracker/0qB76000000GzO4EAK" }, "Id": "003C000002QkzYSIAZ", "OwnerId": "005760000010oRMAAY", "CreatedDate": "2022-10-04T05:42:52.000+0000", "CreatedById": "005760000010oRMAAY", "ActionCadenceId": "77C760000004CBEEA2", "TargetId": "' + testContact.Id + '", "CompletionDisposition": "Future Prospect", "State": "Complete" }]';
      List<ActionCadenceTracker> cadenceList = (List<ActionCadenceTracker>)JSON.deserialize(jsonObject, List<ActionCadenceTracker>.class);

       
       // Begin test context
       Test.startTest();

       // Create a mock UPDATE change event
       EventBus.ChangeEventHeader updateHeader = new EventBus.ChangeEventHeader();
       updateHeader.recordIds = new List<String>{ '003C000002QkzYSIAZ' };
       updateHeader.changeType='UPDATE';
       updateHeader.entityName='ActionCadenceTracker';
       updateHeader.changeOrigin='user1-wsl';
       updateHeader.transactionKey = 'key';
       updateHeader.commitUser = 'user1';
       ActionCadenceTrackerChangeEvent updateEvent = new ActionCadenceTrackerChangeEvent();
       updateEvent.changeEventHeader = updateHeader;
       updateEvent.State = 'Complete';
       updateEvent.CompletionDisposition = 'Future Prospect';
 
       EventBus.publish(updateEvent);

        Test.getEventBus().deliver();

       // Stop test context
       Test.stopTest();

       // Check the details of the published event
       if (!EventBusWrapper.events.isEmpty()) {
           CadencetrackerEvent__e publishedEvent = (CadencetrackerEvent__e) EventBusWrapper.events[0];
           System.assertEquals('Expected TargetId', publishedEvent.TargetId__c, 'TargetId__c does not match.');
           System.assertEquals('Expected State', publishedEvent.State__c, 'State__c does not match.');
           System.assertEquals('Expected CadenceId', publishedEvent.CadenceId__c, 'CadenceId__c does not match.');
           System.assertEquals('Expected RelatedToId', publishedEvent.RelatedToId__c, 'RelatedToId__c does not match.');
           System.assert(EventBusWrapper.events.size() > 0, 'No events were published');
                   
       } 
   }  

Any Help will be highly appreciated

0

You must log in to answer this question.

Browse other questions tagged .