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
DIVAKAR BABU 15DIVAKAR BABU 15 

write a test class for this below trigger

 /**
        trigger fired on:- before delete
        purpose :- restrict users not to delete Casecomments except System admin profile Users         
      **/
trigger restrictUsersFromDel on Case_Comment__c (before delete) {
if(trigger.isBefore && trigger.isdelete){
    Profile SystemAdminId=[select id from Profile where Name='System Administrator' limit 1];
    if(UserInfo.getProfileId() != SystemAdminId.Id){
        for(Case_Comment__c  oldcase:Trigger.old){
                oldcase.adderror('You are not authorized to delete the record , please contact your system administrator');
        }
     }
}
Khan AnasKhan Anas (Salesforce Developers) 
Hi Divakar,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
@isTest
public class trgr_Case_CaseCommentLock_Test{
    
    static Case tCase;
    static CaseComment tComment;
    static Profile tProfile;
    static User tUser;
    
    static void createTestData(){
        tCase = new Case();

        tCase.Status = 'Open';
        tCase.Description = 'Test Description';
        tCase.Origin = 'Annuity External';
        tCase.Type = 'Feature Request';
        tCase.Business_Impact__c = 'No Impact';
        tCase.Priority = 'Low';
      
        INSERT tCase;
        
        tComment = new CaseComment();
        tComment.ParentId = tCase.Id;
        tComment.CommentBody = 'Some Comment';
        tComment.IsPublished = TRUE;
        
        INSERT tComment;
        
        tProfile = 
            [
                SELECT Id 
                FROM   Profile 
                WHERE  Name = 'System Admin'
            ];
            
        tUser = new User(
            Alias = 'standt', 
            Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', 
            LastName='Testing', 
            LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', 
            ProfileId = tProfile.Id, 
            TimeZoneSidKey='America/Los_Angeles', 
            UserName='testuser@sometestorg.com'
        );
        
        INSERT tUser;       
    }
    
    
    @isTest
    static void execCaseCommentDelete(){
        test.startTest();
        
            createTestData();
            
            try{
                System.runAs(tUser){
                    DELETE tComment;
                }
            }
            catch(Exception e){ }
            
        test.stopTest();
    }    
}

Remember to replace - System Admin with the name of a Profile in your Org that has access to Case(Read/Write etc). And replace CaseComment if you are using a custom object.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas