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
Ragnar Lothbrok 7Ragnar Lothbrok 7 

Test class for file upload Class Apex

Hello, could you give me a guide how to build a test class for the apex class I made. I never did code test classes, so i would be very thankful :D
Apex code:
public with sharing class PdrCustomFileUploadctrl {
    @AuraEnabled
    public static String uploadFile(String base64, String filename, String recordId) {
            ContentVersion cv = createContentVersion(base64, filename);
            ContentDocumentLink cdl = createContentLink(cv.Id, recordId);
            if (cv == null || cdl == null) { return null; }
            return cdl.Id;
    }
    private static ContentVersion createContentVersion(String base64, String filename) {
        ContentVersion cv = new ContentVersion();
        cv.VersionData = EncodingUtil.base64Decode(base64);
        cv.Title = filename;
        cv.PathOnClient = filename;
        try {
        insert cv;
        return cv;
        } catch(DMLException e) {
        System.debug(e);
        return null;
        }
    }
    private static ContentDocumentLink createContentLink(String contentVersionId, String recordId) {
                if (contentVersionId == null || recordId == null) { return null; }
        ContentDocumentLink cdl = new ContentDocumentLink();
        cdl.ContentDocumentId = [
        SELECT ContentDocumentId 
        FROM ContentVersion 
        WHERE Id =: contentVersionId 
        WITH SECURITY_ENFORCED
        ].ContentDocumentId;
        cdl.LinkedEntityId = recordId;
        try {
        insert cdl;
        return cdl;
        } catch(DMLException e) {
        System.debug(e);
        return null;
        }
    }
}

 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Ragnar,

We can't able to call the private methods in the test class.

at class level, you need to mention the @testvisible annatation for the private methods.

modify your class as like below.
public with sharing class PdrCustomFileUploadctrl {
    @AuraEnabled
    public static String uploadFile(String base64, String filename, String recordId) {
            ContentVersion cv = createContentVersion(base64, filename);
        system.debug('cv=='+cv);
            ContentDocumentLink cdl = createContentLink(cv.Id, recordId);
            if (cv == null || cdl == null) { return null; }
            return cdl.Id;
    }
    @testvisible
    private static ContentVersion createContentVersion(String base64, String filename) {
        ContentVersion cv = new ContentVersion();
        cv.VersionData = EncodingUtil.base64Decode(base64);
        cv.Title = filename;
        cv.PathOnClient = filename;
        try {
        insert cv;
        return cv;
        } catch(DMLException e) {
        System.debug(e);
        return null;
        }
    }
    @testvisible
    private static ContentDocumentLink createContentLink(String contentVersionId, String recordId) {
                if (contentVersionId == null || recordId == null) { return null; }
        ContentDocumentLink cdl = new ContentDocumentLink();
        cdl.ContentDocumentId = [
        SELECT ContentDocumentId 
        FROM ContentVersion 
        WHERE Id =: contentVersionId 
        WITH SECURITY_ENFORCED
        ].ContentDocumentId;
        cdl.LinkedEntityId = recordId;
        try {
        insert cdl;
        return cdl;
        } catch(DMLException e) {
        System.debug(e);
        return null;
        }
    }
}



try with below test class getting 85% coverage.
@istest
public class PdrCustomFileUploadctrlTest {
    static testmethod void method1(){
        
    string base = 'tesinigmydocument';
    string filename = 'test file';
    Account acc = new Account();
    acc.Name = 'test';
    insert acc;

    string accid = acc.id;
    
    PdrCustomFileUploadctrl.uploadFile(base,filename,accid);       
    }

If this helps, Please mark it as best answer.

thanks!!