2

I'm trying to get my test coverage up and I'm having a few problems to get it done. I have a trigger that will call a class every time a user inserts or updates an event. If that event contains an attachment and the event is related to an account, a new FeedItem will be send to the Account's followers and the Event Owner's followers.
All this logic is working and now the time to test has come ... it has been a pain so far. This is the after insert class, after update pretty much the same, that is been called from the trigger:

public class EventAfterInsertClass {

   public static void eventAfterInsert()
  {

    List<Event> eventList = new List<Event>();
    List<FeedItem> feedList = new List<FeedItem>();
    List<Event> eventToUpdate = new List<Event>(); 
    Set<Id> ownerIds = new Set<Id>();   
    Set<Id> accountIds = new Set<Id>(); 
    Id eventID;

for(Event e : (List<Event>) trigger.new)
{

 if(e.WhatId !=null && e.WhatId.getSObjectType() == Account.SObjectType)
 {
    eventList.add(e); 
  }
}

 Map<Id,Event> eventAttMap = new Map<Id, Event>([SELECT id, OwnerId, Account.Id, has_Attachment__c, (SELECT Id FROM Attachments) FROM Event WHERE Id IN : eventList]); 

for(Event currentEvent :eventAttMap.values())
{

    for(Attachment a :currentEvent.Attachments)
    {

       if(currentEvent.Attachments != null && currentEvent.has_Attachment__c== false)
         {
            currentEvent.has_Attachment__c = true; 
            ownerIds.add(currentEvent.OwnerId);
            accountIds.add(currentEvent.Account.Id);
            eventId = currentEvent.id; 
            eventToUpdate.add(currentEvent);
         }
    }   
}update eventToUpdate; 



List<EntitySubscription> entityListAccount = [SELECT Id, ParentId, SubscriberId FROM EntitySubscription WHERE ParentId IN: accountIds]; 
List<EntitySubscription> entityListOwners = [SELECT Id, ParentId, SubscriberId FROM EntitySubscription WHERE ParentID IN: ownerIds];

for(EntitySubscription entityAccount : entityListAccount)
{

if(!entityListAccount.isEmpty() && entityListAccount.size() >0)
{

        FeedItem accountFeedItem = new FeedItem(
        ParentId = entityAccount.SubscriberId,
        Body = 'This is the msg',
        LinkUrl = 'https://???.salesforce.com/'+ eventId);

        feedList.add(accountFeedItem);

    }

}

for(EntitySubscription entityOwners : entityListOwners)
{

if(!entityListOwners.isEmpty() && entityListOwners.size()>0)
{

       FeedItem accountFeedItem = new FeedItem(
       ParentId = entityOwners.SubscriberId,
       Body = 'This is the msg',
       LinkUrl = 'https://???.salesforce.com/'+ eventId);

       feedList.add(accountFeedItem);
   }
}

if(!feedList.isEmpty() && feedList.size() >0){
   try 
   {
    insert feedList; 
   } 
   catch (DMLException ex)
   {
   }
  }
 }
}

This is the test class I'm working on:

@isTest
 private class TestEventAfterInsert {

public static testMethod void testEventAfterInsertUpdate()
{

    List<Event> eventList = EventCentralizeObjectInitialization.InitTestInsertEvents(2); 
    List<Attachment> attList = new List<Attachment>();
    List<FeedItem> feedList = new List<FeedItem>();
    List<Event> eventToUpdate = new List<Event>();
    Set<Id> ownerIds = new Set<Id>();
    Set<Id> accountIds = new Set<Id>();
    Set<Id> eventAttIds = new Set<Id>(); 
    Id eventID;

    Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');

    Test.StartTest();
    insert eventList;
    Test.StopTest(); 

    Map<Id, Event> eventmap = new Map<Id, Event>(eventList);
    List<Event> events = [SELECT Id, OwnerId, Account.Id, has_Attachment__c, (SELECT Id FROM Attachments) FROM Event WHERE Id IN :eventmap.keySet()];   

    System.assertEquals(events[0].has_Attachment__c, false);
    System.assertEquals(events.size(), 2); 


    for(Event e: events)
    {
        if(e.Id!=null){
            attList.add(new Attachment(
            Name='The test attachment',
            Body=bodyBlob,
            ParentId= e.id,
            OwnerId = e.OwnerId));

        }

    }

    if(!attList.isEmpty() && attList.size()>0)
    {
        insert attList;

        System.assertNotEquals(attList.size(), 0);
        for(Integer x=0; x<events.size(); x++)
        {
        System.assertNotEquals(events[x].has_Attachment__c, true);  
        }

    }

    for(Attachment a: attList)
    {
        if(!attList.isEmpty())
        {
            Id eventIdAtt = a.ParentId;
            eventAttIds.add(eventIdAtt);                
            }
        }

        List<Event> eventListAttIds = [SELECT Id, OwnerId, Account.Id, has_Attachment__c FROM Event WHERE Id IN: eventAttIds];  

        for(Event e: eventListAttIds)
        {
            if(!eventListAttIds.isEmpty() && eventListAttIds.size()>0)
            {

                ownerIds.add(e.OwnerId);
                accountIds.add(e.Account.Id);
                eventId = e.Id;
                eventToUpdate.add(e);

                System.assertNotEquals(ownerIds.size(), 0); 
                System.assertNotEquals(accountIds.size(), 0); 
            }   
                update eventToUpdate;
        }
        System.assertEquals(eventToUpdate[0].has_Attachment__c,true);

     }       
}

This is just giving me a 47% for the AfterInsert and 62% for the AfterUpdate. In both classes (Afterinsert and AfterUpdate) I can't find out how to test those FeedItems. I have been trying a few things for the last couple of days but still can't get them up from 47% and 62% ... could anyone give some indications on how I can test this to accomplish a higher percentage? Thanks ... I'm close to brain damage by now :( ....

0

From looking at your trigger code and the following two schema diagrams, I have a couple of ideas on what needs to be done.

First, your trigger makes these two queries:

List<EntitySubscription> entityListAccount = [SELECT Id, ParentId, SubscriberId FROM EntitySubscription WHERE ParentId IN: accountIds]; 
List<EntitySubscription> entityListOwners = [SELECT Id, ParentId, SubscriberId FROM EntitySubscription WHERE ParentID IN: ownerIds];

Here's a Chatter Feed related schema diagram from the ObjectReference:

Chatter Feed Objects Schema

And here's a 2nd Chatter Objects related schema diagram from the ObjectReference:

Chatter Objects Schema

Your trigger also has the following If statments:

if(!entityListAccount.isEmpty() && entityListAccount.size() >0)
if(!entityListOwners.isEmpty() && entityListOwners.size()>0)
if(!feedList.isEmpty() && feedList.size() >0){

From all of the above, it would appear that you need at a minimum to create EntitySubcriptions for the accounts and for the OwnerIds you've created which would require you to create additional users as well.

I'd also think you'd want to be running queries to verify that your trigger was successful in creating the FeedItems along with adding assertions as to their content. The same would be said with respect to the attachments. I think this should point you in the right direction. I highly recommend you refer to the ObjectReference for each of the objects related to your trigger to see how they interact and what's needed to create or update them.

3
  • Hi crmprogdev, Thanks for your reply, I will be trying this today and let you know how it went :) ... thanks. Jul 2 '15 at 8:07
  • Hello crmprogdev, ... I did what you told me on my Centralize Object Initialization class and now the After Insert is up to 98%, missing one line. The problem I'm facing is now is how to insert a new event with a new attachment assigned to it if I need the event id for that attachment first :( ... The event id wouldn't be available for me until insertion for the event is completed.... Jul 3 '15 at 11:24
  • Glad you got your coverage up. As for the event, it's the same sort of thing, you'd need to create the event, then attach to it. Preparing data for test classes can be quite tedious at times...
    – crmprogdev
    Jul 3 '15 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.