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
TylerBrooksTylerBrooks 

FeedItem Test Class not giving proper coverage

Hey All,

I wrote a simple apex trigger to get Case Feed Items and after they're inserted create a new record which stores information for our Business analysts. The code works in my sandbox and does what I want it to do but when trying to create a test class I only get 18% coverage and I'm not sure how to adjust my test class to get the required code coverage.

TRIGGER: 
trigger CaseFeed on FeedItem (after insert) 
{
    List<Case> Cases = [SELECT Id, CaseNumber FROM Case];
    for(case myCase : Cases)
    {
    	for(FeedItem caseFeed : trigger.new)
    	{
        	if(caseFeed.ParentId == myCase.Id)
        	{
            	Case_History_and_Feed_Tracking__c chf = new Case_History_and_Feed_Tracking__c();
            	chf.Case__c = caseFeed.ParentId;
            	chf.TSR__c = caseFeed.CreatedById;
            	chf.Change_Made_Body__c = caseFeed.Body;
				insert chf;
        	}
    	}
    }

}

TEST CLASS:
@isTest
private class TestCaseFeed
{
    @isTest static void InsertCaseFeed()
    {
		FeedItem caseFeed = new FeedItem();
        caseFeed.Body= 'Testing my test class :D';
        caseFeed.ParentId = '5002a000004TOCrAAO';
        caseFeed.CreatedById = '0050y00000E1zyyAAB';
        insert caseFeed;
    }

}
The lines of code in the trigger saying they get coverage is
List<Case> Cases = [SELECT Id, CaseNumber FROM Case];
    for(case myCase : Cases)
Best Answer chosen by TylerBrooks
Raj VakatiRaj Vakati
try this
@isTest
private class TestCaseFeedTest {
    static testMethod void unitTestInsert() {
    Case cse = new Case (STATUS = 'New', ORIGIN = 'Web', TYPE='Claims', SUBJECT = 'Test', DESCRIPTION= 'Testing');
     Insert cse;
     FeedItem feed = new FeedItem (
            parentid = cse.id,
            type = 'ContentPost',
            Body = 'Hello');
			insert feed ; 
			
    }
}

 

All Answers

Raj VakatiRaj Vakati
try this
@isTest
private class TestCaseFeedTest {
    static testMethod void unitTestInsert() {
    Case cse = new Case (STATUS = 'New', ORIGIN = 'Web', TYPE='Claims', SUBJECT = 'Test', DESCRIPTION= 'Testing');
     Insert cse;
     FeedItem feed = new FeedItem (
            parentid = cse.id,
            type = 'ContentPost',
            Body = 'Hello');
			insert feed ; 
			
    }
}

 
This was selected as the best answer
TylerBrooksTylerBrooks
Thanks Raj, I'd also tried to create a case and make the parent Id the created Cases Id but it gave me 0 coverage, I think I was forgetting content type so it was failing. But yours worked so thank you!