function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Damien BellDamien Bell 

Why isn't my apex test covering my trigger's if statement?

Hi All,

I have a Apex Class which looks like this:
 
trigger FirstTouch on FeedComment (after insert, after update){
    List<Case> updates = new List<case>();
    List<Id> feedItemList = new List<id>();
    Map<Id, User> userMap = new Map<Id, User>([SELECT Id FROM User WHERE Support_Team_Member__c = true]);
    for(FeedComment fc: trigger.new){
        if(userMap.containsKey(fc.InsertedById)){
            feedItemList.add(fc.ParentID);
        }
    }
    
    for(Case c : [Select Id from Case where Id IN (Select ParentId FROM FeedItem WHERE Id IN: feedItemList)]){
        if(c.Support_First_response_minutes__c == null && c.Case_Assigned_To_Support__c != null){
            long totalMs =     DateTime.now().getTime() - c.Case_Assigned_To_Support__c.getTime();
            totalMs *= 60000;
            updates.add(new Case(
                id = c.Id,
                Support_First_response_minutes__c = totalMs
            ));
       }
     }    
    update updates;
}


 
My test looks like this:
 
@isTest
    private class firstTouch {
        private static testMethod void firstTouch(){
            //Create a new test case
            Account a = new Account(name='test acc',phone='7777777777');
            insert a;
            Contact con = new Contact(accountid=a.id,email='test@test.com');
            con.FirstName = 'Testy';
            con.LastName = 'mcTest';
            insert con;
            Case c = new Case(Status = 'New', Priority = 'Medium', Description = 'Test', Last_Updated_By_Support__c = System.now(), Contact = con,
                                     Case_Assigned_To_Support__c = System.now(),
                                     Support_First_response_minutes__c = null );
            insert c;
            if(c.Support_First_response_minutes__c == null && c.Case_Assigned_To_Support__c!= null ){
                //Create a new feed item (Post) on the test case
                Feeditem fi = new feeditem();
                fi.Body = 'test Post on case';
                fi.Type = 'TextPost';
                //Create a new comment on the post in the case
                fi.ParentId = c.Id;
                insert fi ;
                FeedComment fc = new FeedComment(CommentBody = 'test', FeedItemID = fi.Id);
                fc.CommentType = 'TextComment';
                insert fc ;
            }
        }
    }


I've tried adding in an else as well as a 2nd method -- and I can't seem to get my coverage above 53%  Can someone help with this?
Andrew GAndrew G
Hi Damien

I belive this line is your issue:
 if(userMap.containsKey(fc.InsertedById)){

The Test class will be running as your user account and you (possibly) don't have the "Support_Team_Member__c" check box on your user profile.

Look into creating a test user in your code and assigning that check box and then use the RunAs 
This example should give a good start point. 
https://developer.salesforce.com/forums/?id=906F0000000BPkhIAG

Note, for code that relies on a user permission, you should be using the RunAs method.

Regards
Andrew
chanchal_:)chanchal_:)
On line number 11 use query - [Select Id, Support_First_response_minutes__c, Case_Assigned_To_Support__c from Case where Id IN (Select ParentId FROM FeedItem WHERE Id IN: feedItemList)]

In your test class just insert relevant data, without using any if statements.