0

I have created a trigger that will create a Case Comment when a custom object record is created and I cannot get any Code Coverage on the Trigger. What am I missing...I feel like it is something simple.

Trigger:

trigger createSupportComment on CaseComment (after insert) {

for (CaseComment caseC : Trigger.new) {

       if(caseC.CommentBody != null && caseC.isPublished == true && System.Userinfo.getUserType() != 'Standard'){ 

          string cc = caseC.CommentBody;

          Support_Comment__c supC = new Support_Comment__c(Case__c = caseC.ParentId);
          supC.Comment__c = caseC.CommentBody;
          supC.public__c = true;

           insert(supC);
       }
  }

}

Test Code:

@istest(seeAllData = True)
public class createSupportCommentTest {

static testMethod void createCommentTest(){

    Case c = new Case();
        insert c;

    list<user> UserList = [SELECT id, UserType FROM user WHERE UserType != 'Standard' LIMIT 1];

    CaseComment cc = new CaseComment();
    cc.ParentId = c.Id;
    cc.CommentBody = 'Hello';
    cc.CreatedById = UserList[0].id;
    cc.IsPublished = True;
    insert cc;

    List<Support_Comment__c> supC = [SELECT Id, Case__c, Comment__c FROM Support_Comment__c WHERE Case__c =:c.Id];

    for(Support_Comment__c sC:supC){
        System.assertEquals(cc.CommentBody,sC.Comment__c);
    }
}

}

Any help would be much appreciated. Thanks

  • In your test you use UserType of Standard User, whereas your trigger uses a UserType of Standard. Are you sure you have any nonstandard Users? If not, you will have to create them. – Adrian Larson Jan 6 '16 at 0:09
  • Those should both read 'Standard'. I have fixed that and update the above code as such and still do not have any coverage. I am positive that there are nonstandard users within the environment. Thanks for the comment. – user25814 Jan 6 '16 at 0:25
  • I take that back...for some reason when I first looked at the code coverage after making that change it showed 0, but now it is showing 28%. Getting closer. – user25814 Jan 6 '16 at 0:45
  • Best practice - do not use SeeAllData=true. You or your successor may come to a future deployment failure. User data should be mocked in your testmethod – cropredy Jan 6 '16 at 2:26
2

Without modifying your test at all, you can increase your coverage significantly:

trigger createSupportComment on CaseComment (after insert) {
    Support_Comment__c[] newComments = new Support_Comment__c[0];
    for (CaseComment caseC : Trigger.new) {
        if(caseC.CommentBody != null && caseC.isPublished && System.Userinfo.getUserType() != 'Standard'){ 
          newComments.add(new Support_Comment__c(Case__c = caseC.ParentId, Comment__c = caseC.CommentBody, public__c = true));
       }
     insert newComments;
  }
}

This will increase your coverage to 80%. To get the last 20%, you need to use System.runAs to test as that user:

System.runAs(UserList[0]) {
    insert cc;
}
  • I have a question on the way you created the List of type 'Support_Comment__c'. May I know the reason why you created list collection in array style by specifying the size as '0' but then again when you add objects, you are using List add methods which makes it elastic. Just wanted to know if there is any advantage in creating list like you did than regular approach for eg: new List<Support_Comment__c>(); – javanoob Jan 6 '16 at 3:16
  • 1
    @javanoob I'm actually investigating it for my blog, but I suspect that the answer is "there's no material difference". That said, it's fewer characters to type, so it's a little faster to type. I'm the sort of person that doesn't want to type more than necessary. Also: 3 million character limit. Using the long form does use a fairly limited resource. – sfdcfox Jan 6 '16 at 4:09
  • Thank you very much. I have made the adjustments and am now at 100% – user25814 Jan 6 '16 at 14:52

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.