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
Hitesh KhannaHitesh Khanna 

How to write test class for ContentDocument file retrieval class ?

    public static String getContentDetails(String recordId) {
        List<ContentDocumentLink> contentDocumentList = [SELECT ContentDocumentId, LinkedEntityId 
                                                            FROM   ContentDocumentLink 
                                                            WHERE  LinkedEntityId =: recordId];
        Set<Id> contentDocumentId = new Set<Id>();
        for(ContentDocumentLink cdl : contentDocumentList){
        List<ContentVersion> contentVersionList = [SELECT Id, VersionData, FileType, Title, FileExtension,Last_Updated_Date__c,
                                                    Comments__c,Last_Updated__c,Baseline_Category__c,CreatedBy.Name, ContentDocument.ContentSize,
                                                    CreatedDate, ContentDocumentId, ContentDocument.FileType
                                                    FROM   ContentVersion 
                                                    WHERE  ContentDocumentId IN : contentDocumentId order by Last_Updated_Date__c DESC ];
        return JSON.serialize(contentVersionList);
This is my method for which i want to write test class
Hii Hitesh
Try Below Test Class
Public class TestShareFilesWithCommunityUsers {
    Public static void testmethod1(){
        Account accA =new Account(Name='Demo');
        insert accA ; 
        ContentVersion content=new ContentVersion(); 
        content.PathOnClient='/' + content.Title + '.jpg'; 
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 
        content.origin = 'H';
        insert content;
        ContentDocumentLink contentlink=new ContentDocumentLink();;
        contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =:].contentdocumentid;
        contentlink.ShareType = 'I';
        contentlink.Visibility = 'AllUsers'; 
        insert contentlink;
Please Mark It As Best Answer If It Helps
Thank You!
ravi soniravi soni
hi Hitesh Khanna,
here is your test class with 100% coverage.
public class getContentDetailsCtrlTest {
    public static void testUnit(){
        //Create Account
        Account acct = new Account();
        //Write all required Fields like this
        insert acct;
        //Create contentVersion
        ContentVersion cv = new ContentVersion();
        cv.Title = 'Test Document';
        cv.PathOnClient = 'TestDocument.pdf';
        cv.VersionData = Blob.valueOf('Test Content');
        cv.IsMajorVersion = true;
        Insert cv;
        //Get Content Version
        List<ContentVersion> cvList = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id];
        System.assertEquals(cvList.size(), 1);
        system.debug('ContentVersionID : ' + cvList[0].Id);
        //create ContentDocumentLink  record 
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId =;
        cdl.ContentDocumentId = cvList[0].ContentDocumentId;
        cdl.shareType = 'V';
        insert cdl;  
        string sContentVersionJson =  getContentDetailsCtrl.getContentDetails(;

don't forget mark it as best answer if it helps you.
Thank you
Hitesh KhannaHitesh Khanna
Hi Charu,
Thanks for sol but getting errors :
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountTrigger: execution of BeforeInsert

caused by: TriggerException: No Trigger Handler custom setting found for Object Type: Account

Class.TriggerFactory.createHandler: line 33, column 1
Trigger.AccountTrigger: line 8, column 1: []

Hii Hitesh
You Need To Insert Custom Setting For Account In Test Class As Shown in Below Example:
insert new CustomSetting__c(Field__c = 'Value');

Nelson NolandNelson Noland
Thanks for the example test codes. I used them for a small document with the convolution of the problem answer, took an example from with a ready-made solution and added to the tables. Extremely convenient and simple solution turned out to simplify a lot of the process for me. I think to make my own tool with a set of similar functionality and try it out, but it's not soon.