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
RuniRuni 

casecomment

Hi all,

How write test class for below trigger.


trigger Comment on CaseComment (before insert){
Set<Id> parentCase=new Set<Id>();
Map<Id,Case> mapCase=new Map<Id,Case>();
for (CaseComment t: Trigger.new){
parentCase.add(t.ParentId);
}
List<Case> lstCase=[Select Id,Status,Owner.name,Owner.Type from case where Id in :parentCase];
for(case c :lstCase){
mapCase.put(c.Id,c);
}
for (CaseComment t: Trigger.new){
if(mapCase.containskey(t.ParentId))
{

if( mapCase.get(t.ParentId).owner.Type=='Queue')
    {
        t.addError('please add comment.');     
    }
    
}
}
}
CharuDuttCharuDutt
Hii Swathi Reddy
Try below Test Class 90% Coverage!
@isTest
public class CommentTest {
@isTest
    public Static Void Unittest(){
        Account Acc = new Account();
        Acc.Name = 'Tests Account';
        Insert Acc;
        Case c =new Case();
        c.Origin = 'Web';
        c.Subject = 'Test';
        c.Status = 'New';
        c.AccountId = Acc.id;
        Insert c;
        
        CaseComment newCommmand = new CaseComment();       
        newCommmand.IsPublished = TRUE;
        newCommmand.ParentId = c.id;
        Insert newCommmand;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Swathi,

Can you please use the below testclass. It covers 100%. Make sure you fill all mandatory fields while creating case.
 
@isTest
public class CommentTest {
       static Case tCase;
    static CaseComment tComment;
static testMethod void myUnitTest(){
    Group testGroup = new Group(Name='test group', Type='Queue');
insert testGroup;

System.runAs(new User(Id=UserInfo.getUserId()))
{
    QueuesObject testQueue = new QueueSObject(QueueID = testGroup.id, SObjectType = 'Case');
    insert testQueue;
}
        tCase = new Case();
        tCase.Status = 'Open';
        tCase.Description = 'Test Description-1 ';
        tCase.Origin = 'Annuity External';
        tCase.Type = 'Feature Request';
        tCase.Priority = 'Low';
    	tcase.OwnerId=testGroup.id;
        INSERT tCase;
        
        tComment = new CaseComment();
        tComment.ParentId = tCase.Id;
        tComment.CommentBody = 'Some Comment-1';
        tComment.IsPublished = TRUE;
    Database.SaveResult result = Database.insert(tComment, false);
        System.assertEquals('please add comment.',result.getErrors()[0].getMessage());
    
    
}
}


If this solution helps, Please mark it as best answer.

Thanks,
 
CharuDuttCharuDutt
Hii Swathi Reddy
Try below Test Class 100% Coverage!
@isTest
public class CaseCommentTest {
@isTest
    public static void Unittest(){
        
        Group oGroup = new Group(Name='test group', Type='Queue');
insert oGroup;

System.runAs(new User(Id=UserInfo.getUserId()))
{
    QueuesObject testQueue = new QueueSObject(QueueID = oGroup.id, SObjectType = 'Case');
    insert testQueue;
}
        Account Acc = new Account();
        Acc.Name = 'Tests Account';
        Insert Acc;
        Case c =new Case();
        c.Origin = 'Web';
        c.Subject = 'Test';
        c.Status = 'New';
        c.AccountId = Acc.id;
        c.OwnerId=oGroup.id;
        Insert c;
        
        CaseComment newCommmand = new CaseComment();
        
        newCommmand.IsPublished = TRUE;
        newCommmand.ParentId = c.id;
        Insert newCommmand;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
RuniRuni
Hi Chardutt,

thanks for the helip but code coverage is 100 but test class running fail.
i am getting below error.

Error MessageSystem.AssertException: Assertion Failed: Expected: please add comment., Actual: please add comment.
CharuDuttCharuDutt
Hii Swathi
Try Below Code I've Changed Assertion
@isTest
public class CaseCommentTest {
@isTest
    public static void Unittest(){
        
        Group oGroup = new Group(Name='test group', Type='Queue');
insert oGroup;

System.runAs(new User(Id=UserInfo.getUserId()))
{
    QueuesObject testQueue = new QueueSObject(QueueID = oGroup.id, SObjectType = 'Case');
    insert testQueue;
}
        Account Acc = new Account();
        Acc.Name = 'Tests Account';
        Insert Acc;
        Case c =new Case();
        c.Origin = 'Web';
        c.Subject = 'Test';
        c.Status = 'New';
        c.AccountId = Acc.id;
        c.OwnerId=oGroup.id;
        Insert c;
        
        CaseComment newCommmand = new CaseComment();
        
        newCommmand.IsPublished = TRUE;
        newCommmand.ParentId = c.id;
        Insert newCommmand;
 System.assertEquals(Acc.Id,c.AccountId);


    }
}
Please Close your Query By  Marking It As Best Answer If It Helps So It Also Helps Others in Future
Thank You!