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
Sharmila Sahu 20Sharmila Sahu 20 

We have one trigger in org on sobject type Delete Content Document. I am not able to write test class for it.

We have one trigger in org on sobject type Delete Content Document. I am not sure what is this object and how to create its record in test class.I am not able to write test class for it.

Please find below code of trigger:
trigger DeleteContentDocumentTrigger on Delete_Content_Document__e (after insert) {
    List<ContentDocument> contentDocumentList = new List<ContentDocument>();
    List<Attachment> attachmentList = new List<Attachment>();
    List<eLicense_Attachment__c> eLicenseAttachmentList = new List<eLicense_Attachment__c>();
       
    for (Delete_Content_Document__e event : Trigger.New) {
        if (event.Content_Document_Id__c != null) {
            contentDocumentList.add(new ContentDocument(id = event.Content_Document_Id__c));
        }
        if (event.Attachment_Id__c != null) {
            attachmentList.add(new Attachment(id = event.Attachment_Id__c));
        }
        if (event.eLicense_Attachment_Id__c != null) {
            eLicenseAttachmentList.add(new eLicense_Attachment__c(id = event.eLicense_Attachment_Id__c));
        }
   }
    
    delete contentDocumentList;
    delete attachmentList;
    delete eLicenseAttachmentList;
}

Please find below test class:
@isTest
public class DeleteContentDocumentTriggerTest {
    
    public static testmethod void test_contentDoc(){
        Test.startTest();
          //try{
    Global_Settings__c globalSettings = new Global_Settings__c(Name = 'Default', Disable_Triggers__c = true);
        if(globalSettings != null) insert globalSettings;
        Account acc = OH_Utility_UnitTestData.createTestAccount();
        insert acc;
        Blob beforeblob=Blob.valueOf('Unit Test Attachment Body');

        ContentVersion cv = new ContentVersion();
        cv.title = 'test content trigger';      
        cv.PathOnClient ='test';           
        cv.VersionData =beforeblob;          
        insert cv;             
                                                
         List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];

        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=acc.id;
        contentlink.ShareType= 'V';
        contentlink.ContentDocumentId=documents[0].Id;
        contentlink.Visibility = 'AllUsers'; 
        insert contentlink;
    
      
         delete documents;
        
         delete contentlink;
         Delete_Content_Document__e dc = new Delete_Content_Document__e();
       // EventBus.publish(dc);
            
        //}
        //catch(Exception e){}
        Test.stopTest();
    //insert dc;
    }

}
It is not covering trigger at all.
AnudeepAnudeep (Salesforce Developers) 
Hi Sharmila, 

Your trigger is on a Platform Event. When you create a platform event, the system appends the __e suffix to create the API name of the event. See Define and Publish Platform Events  to learn more

This Sample Test Class  will guide you in achieving the coverage you are looking for

If you find this information helpful, please mark this as solved by selecting this answer as best so that it may help others in the future. Thank You!

Anudeep