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
Shaun B.Shaun B. 

Need code coverage

Hi there!

I am new to Apex, but would like to deploy the following Apex code to my org.  The issue however is that I don't know how to write unit tests.  Can anyone assist?
 
trigger UpdateCaseCommentFields on FeedItem (after insert) {
    List<Case> updates = new List<Case>();
    for (FeedItem fi : Trigger.new) {
        if (fi.ParentId.getSObjectType() == Case.SObjectType && fi.Visibility == 'InternalUsers') {
            updates.add(new Case(
                    Id = fi.ParentId,
                    Last_Internal_Comment__c = fi.Body
                    ));
        }
        else if (fi.ParentId.getSObjectType() == Case.SObjectType && fi.Visibility != 'InternalUsers') {
            updates.add(new Case(
                    Id = fi.ParentId,
                    Last_External_Comment__c = fi.Body
                    ));
        }
    }
    update updates;
}

 
Best Answer chosen by Shaun B.
AshlekhAshlekh
Hi,

First need to put a validation in your code to check the size of list updates list.
 
if(updates != null && updates.size()>0)
  update updates;

And Need to write test class for +ive and -ive cases. 
 
@isTest
private class UpdateCaseCommentFieldsTest {

    static testMethod void myUnitTest1() {
        Case caseObj = new Case (Status = 'Open', Priority = 'Low', Origin = 'Email');
        insert caseObj;
       Test.startTest();
        FeedItem feed =  new FeedItem ();
        feed.ParentId = caseObj.Id;
        feed.Body = 'Hello Internal';
        feed.Visibility = 'InternalUsers';
        insert feed;
        feed =  new FeedItem ();
        feed.ParentId = caseObj.Id;
        feed.Body = 'Hello All';
        feed.Visibility = 'AllUsers';
        insert feed;
//if you can user system assert than it will be good
        test.stopTest();    
}

    static testMethod void myUnitTest2() {
        Case caseObj = new Case (Status = 'Open', Priority = 'Low', Origin = 'Email');
        insert caseObj;
       Test.startTest();
        FeedItem feed =  new FeedItem ();
        feed.ParentId = caseObj.Id;
        feed.Body = 'Hello Internal';
        feed.Visibility = 'InternalUsers1';
        insert feed;

        feed =  new FeedItem ();
        feed.ParentId = caseObj.Id;
        feed.Body = 'Hello All';
        feed.Visibility = 'AllUsers1';
        insert feed;
        Test.stopTest();    
}

}

-Thanks
Ashlekh Gera
 

All Answers

AshlekhAshlekh
Hi,

First need to put a validation in your code to check the size of list updates list.
 
if(updates != null && updates.size()>0)
  update updates;

And Need to write test class for +ive and -ive cases. 
 
@isTest
private class UpdateCaseCommentFieldsTest {

    static testMethod void myUnitTest1() {
        Case caseObj = new Case (Status = 'Open', Priority = 'Low', Origin = 'Email');
        insert caseObj;
       Test.startTest();
        FeedItem feed =  new FeedItem ();
        feed.ParentId = caseObj.Id;
        feed.Body = 'Hello Internal';
        feed.Visibility = 'InternalUsers';
        insert feed;
        feed =  new FeedItem ();
        feed.ParentId = caseObj.Id;
        feed.Body = 'Hello All';
        feed.Visibility = 'AllUsers';
        insert feed;
//if you can user system assert than it will be good
        test.stopTest();    
}

    static testMethod void myUnitTest2() {
        Case caseObj = new Case (Status = 'Open', Priority = 'Low', Origin = 'Email');
        insert caseObj;
       Test.startTest();
        FeedItem feed =  new FeedItem ();
        feed.ParentId = caseObj.Id;
        feed.Body = 'Hello Internal';
        feed.Visibility = 'InternalUsers1';
        insert feed;

        feed =  new FeedItem ();
        feed.ParentId = caseObj.Id;
        feed.Body = 'Hello All';
        feed.Visibility = 'AllUsers1';
        insert feed;
        Test.stopTest();    
}

}

-Thanks
Ashlekh Gera
 
This was selected as the best answer
Shaun B.Shaun B.
Thank you both.  Unfortunately I get the following error for both examples:
 
Methods defined as TestMethod do not support Web service callouts