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
Mukesh Kumar 470Mukesh Kumar 470 

please help me how to write test class for below code.

public without sharing class FileUpload_ctrl {

    @AuraEnabled
    public static Id saveFile(Id parentId, String fileName, String base64Data, String contentType, String fileDescription )  { 
        
        ContentVersion contentToInsert = new ContentVersion(); 
        contentToInsert.Title = fileDescription + ' - ' + fileName; 
        contentToInsert.VersionData = EncodingUtil.base64Decode(EncodingUtil.urlDecode(base64Data, 'UTF-8'));
        contentToInsert.PathOnClient = '/' + fileName ;
        contentToInsert.Description = fileDescription;
        contentToInsert.IsMajorVersion = false;
        insert contentToInsert; 
        
        contentToInsert = [select id, ContentDocumentId from ContentVersion WHERE Id =: contentToInsert.Id];
        ContentDocumentLink cl = new ContentDocumentLink();
        cl.ContentDocumentId = contentToInsert.ContentDocumentId;
        cl.LinkedEntityId = parentId; 
        cl.ShareType = 'V';
        cl.Visibility = 'AllUsers';
        insert cl;
        
        return contentToInsert.id;
    }
}
 
Best Answer chosen by Mukesh Kumar 470
CharuDuttCharuDutt
Hii Mukesh
Try Below  Code
@isTest
public without sharing class FileUpload_ctrlTest {

   @isTest
    public static void Unittest()  { 
        Account Acc = new Account ();
        Acc.Name = 'Test Account';
        //Fill All Required fields
        insert Acc;

       Id parentId = Acc.Id; 
       String fileName = 'TestFile';
       String base64Data = EncodingUtil.urlDecode('Test base64Data For testing', 'UTF-8');
       String contentType = 'mp4'; 
       String fileDescription = 'Testing File';
        FileUpload_ctrl.saveFile(parentId,fileName,base64Data,contentType,fileDescription );
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hiii Mukesh
Try Below test Class
@isTest
public without sharing class FileUpload_ctrlTest {

   @isTest
    public static void Unittest(Id parentId, String fileName, String base64Data, String contentType, String fileDescription )  { 
        Account Acc = new Account ();
        Acc.Name = 'Test Account';
        //Fill All Required fields
        insert Acc;

       Id parentId = Acc.Id; 
       String fileName = 'TestFile' 
       String base64Data = EncodingUtil.urlDecode('Test base64Data For testing', 'UTF-8');
       String contentType = 'mp4'; 
       String fileDescription = 'Testing File';
        FileUpload_ctrl.saveFile(parentId,fileName,base64Data,contentType,fileDescription );
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
AbhinavAbhinav (Salesforce Developers) 
Check this:

https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

Thanks!
Mukesh Kumar 470Mukesh Kumar 470
Hi i am trying but showing this type of errorUser-added image
CharuDuttCharuDutt
Hii Mukesh
Try Below  Code
@isTest
public without sharing class FileUpload_ctrlTest {

   @isTest
    public static void Unittest()  { 
        Account Acc = new Account ();
        Acc.Name = 'Test Account';
        //Fill All Required fields
        insert Acc;

       Id parentId = Acc.Id; 
       String fileName = 'TestFile';
       String base64Data = EncodingUtil.urlDecode('Test base64Data For testing', 'UTF-8');
       String contentType = 'mp4'; 
       String fileDescription = 'Testing File';
        FileUpload_ctrl.saveFile(parentId,fileName,base64Data,contentType,fileDescription );
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer