You need to sign in to do that
Don't have an account?
Ragnar 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:
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; } } }
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.
try with below test class getting 85% coverage.
If this helps, Please mark it as best answer.
thanks!!