+ Start a Discussion
Hitesh KhannaHitesh Khanna 

how do i write test class for this method...i tried creating test data setup but i m not getting how to create the recordid...can someone help ?

 public static Map<ID, ContentVersion> getRelatedFilesByRecordId(String recordId) {
        system.debug('recordId-->'+recordId);
        // Get record file IDs        
        List<ContentDocumentLink> files = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :recordId];
        List<ID> fileIDs = new List<ID>();
        for (ContentDocumentLink docLink : files) {
            fileIDs.add(docLink.ContentDocumentId);
        }
        system.debug('fileIDs-->'+fileIDs);
        List<ContentVersion> docs = [SELECT ContentDocumentId, FileExtension, Title, CreatedDate,Document_Name__c,FileType,Comments__c 
            FROM ContentVersion WHERE ContentDocumentId IN : fileIDs Order by CreatedDate DESC ];
        Map<ID, ContentVersion> mapIdTitle = new Map<ID, ContentVersion>();
        for (ContentVersion docLink : docs) {
            mapIdTitle.put(docLink.ContentDocumentId, docLink);
        }
       
        return mapIdTitle;
    }
Suraj Tripathi 47Suraj Tripathi 47

Hi Hitesh,

In this case the recordId will be the id of the object which you have created in setup method.

For example, if you have created an account record named accObj  in setup then you can do the following in your test class to cover the code coverage of this class :

test.starttest();

YourClassName.getRelatedFilesByRecordId(accObj.id);

test.stop();

Please mark it as best answer if it resloves your issue.
Thanks.
Hitesh KhannaHitesh Khanna
@isTest
public class FileControllerTest {
    
    @TestSetup
    static void setupTestData() {
        UnitTestUtilities.GenerateCustomSettings();
         ContentDocumentLink cr = (ContentDocumentLink) new SObjectBuilder(ContentDocumentLink.sObjectType)
            .put(ContentDocumentLink.ContentDocumentId, '001')
            .put(ContentDocumentLink.LinkedEntityId, '000')
            .create()
            .getRecord();
        ContentVersion cvr = (ContentVersion) new SObjectBuilder(ContentVersion.sObjectType)
            .put(ContentVersion.ContentDocumentId, '001')
            .put(ContentVersion.FileExtension, 'exe')
            .put(ContentVersion.Title, 'demo')
            .put(ContentVersion.CreatedDate, '28/09/2021')
            .put(ContentVersion.Document_Name__c, 'abc')
            .put(ContentVersion.FileType, 'pdf')
            .put(ContentVersion.Comments__c, 'correct')
            .create()
            .getRecord();
        
    }
    
    
    @isTest
    public static void getRelatedFilesByRecordIdTest(){
     
        test.startTest();
        FileController.getRelatedFilesByRecordId(cr.id);
   
        test.stopTest();
        
    }

}

I wwrote this...but i am getting "cr variable doesnt exist"
Hitesh KhannaHitesh Khanna
@suraj tripathi47  can u tell what to do here ?
 
Hitesh KhannaHitesh Khanna
what is lettingobj here ? i am getting this error :
User-added image
Hitesh KhannaHitesh Khanna
@suraj tripathi47
 
Suraj Tripathi 47Suraj Tripathi 47

It is the record of object where you want to insert the file. You can create a contact and assign its id in place of lettingobj.

Thanks.

Hitesh KhannaHitesh Khanna
@suraj i think u have not understood my code....can anybody else help me in writing test class for the above mentioned code ?
Suraj Tripathi 47Suraj Tripathi 47
@isTest
public class TestClasstotrrstTest {
    @isTest
    private static void getRelatedFilesByRecordIdTest(){
        
        Contact con = new Contact();
        con.Firstname = 'test';
        con.LastName = 'dsd';
        insert con;
        
        ContentVersion cv = new Contentversion();
        cv.title='ABC';
        cv.PathOnClient ='ABC.jpg';
        cv.versiondata=EncodingUtil.base64Decode('Unit Test Attachment Body');
        insert cv;
        ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];	
        
        ContentDocumentLink contentlink = new ContentDocumentLink();
        contentlink.LinkedEntityId = con.id;
        contentlink.ShareType= 'V';
        contentlink.ContentDocumentId = testcontent.ContentDocumentId;
        contentlink.Visibility = 'AllUsers';
        insert contentlink;
        
        Test.startTest();
//Please write your class name before function.
        TestClasstotrrst.getRelatedFilesByRecordId(con.Id);
        Test.stopTest();
    }
}


Please try this code. Hope this will help you.

It is covering here.

 

Please give it a like if it helps you.

 

User-added image

Hitesh KhannaHitesh Khanna
@suraj....for me the test is getting failed and throwing this error.User-added image
Hitesh KhannaHitesh Khanna
User-added image
also this
Suraj Tripathi 47Suraj Tripathi 47
This is caused by some trigger which you have written on contact. you can insert an account( or any other object) to avoid this issue or you can comment this particular trigger to cover this class.