+ Start a Discussion
Hitesh KhannaHitesh Khanna 

How to write test class for ContentDocument file retrieval class ?

@AuraEnabled
    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){
            contentDocumentId.add(cdl.ContentDocumentId);
        }
            
        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
 
CharuDuttCharuDutt
Hii Hitesh
Try Below Test Class
@IsTest
Public class TestShareFilesWithCommunityUsers {
    @IsTest
    Public static void testmethod1(){
        Test.startTest();
        Account accA =new Account(Name='Demo');
        insert accA ; 
       
        
        ContentVersion content=new ContentVersion(); 
        content.Title='Header_Picture1'; 
        content.PathOnClient='/' + content.Title + '.jpg'; 
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 
        content.VersionData=bodyBlob; 
        //content.LinkedEntityId=sub.id;
        content.origin = 'H';
        insert content;
        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=accA.id;
        contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
        contentlink.ShareType = 'I';
        contentlink.Visibility = 'AllUsers'; 
        
        
        insert contentlink;
        ContentDetails.getContentDetails(accA.id);
        Test.stopTest();
        
    }
}
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.
@isTest
public class getContentDetailsCtrlTest {
@isTest
    public static void testUnit(){
        //Create Account
        Account acct = new Account();
        acct.Name='TEST_ACCT';
        //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 = acct.id;
        cdl.ContentDocumentId = cvList[0].ContentDocumentId;
        cdl.shareType = 'V';
        insert cdl;  
         System.assertEquals(cdl.ContentDocumentId,cvList[0].ContentDocumentId);
            
        test.startTest();
        string sContentVersionJson =  getContentDetailsCtrl.getContentDetails(acct.id);
        test.stopTest();
        
    }
}

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: []

 
CharuDuttCharuDutt
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');