+ Start a Discussion

What kind of test can I write to check the file?

Hello. I have a controller for VF Page. This code upload a profile's image (we can see it after that) and delete it. What kind of test can help me to check this file? I need to know, is it uploaded or not.

I work with ContentVersion  and ContentDocumentLink :

public class FileUploaderController {
    public ContentVersion conVer {get; set;}
    public ContentDocumentLink conDocLink {get; set;}
    public Candidate__c thisCandidate{get; set;}
    public FileUploaderController(ApexPages.StandardController controller){
        thisCandidate = (Candidate__c)controller.getRecord();
        conVer = new ContentVersion();

    public PageReference uploadFile() {
        List<ContentVersion> conVerList = new List<ContentVersion>();
        List<ContentDocumentLink> conDocLinkList = new List<ContentDocumentLink>();
        if (conVer.VersionData == null) {
            conVer.VersionData = null;
            return null;
        insert conVerList;              
        thisCandidate.Photo__c = conVer.Id;
        update thisCandidate; 
        if (conVer.ContentDocumentId == null) {
            Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =: conVer.Id].ContentDocumentId;
            conDocLink = new ContentDocumentLink(
                LinkedEntityId = thisCandidate.Id,
                ContentDocumentId = conDoc,
                ShareType = 'I'
            insert conDocLinkList;
        conVer.VersionData = null;
        return null;

    public PageReference deleteFile() {
        thisCandidate.Photo__c = null;
        update thisCandidate;
        conVer = new ContentVersion();
        return null;
Best Answer chosen by Simon234
Raj VakatiRaj Vakati
Pls use this test class
public class FileUploaderControllerTest {
    static testMethod void validateHelloWorld()
        System.Test.startTest() ;
        Candidate__c can = new Candidate__c(Name = 'Wholesale');
        insert can;
        ApexPages.StandardController sc = new ApexPages.StandardController(can);
        FileUploaderController conT = new FileUploaderController(sc);
        PageReference pageRef = Page.canpage;
        pageRef.getParameters().put('id', String.valueOf(can.Id));
        Blob beforeblob=Blob.valueOf('Unit Test Attachment Body');

        ContentVersion cv = new ContentVersion();
        cv.title = 'test content trigger';      
        cv.PathOnClient ='test';           
        cv.VersionData =beforeblob;          
        insert cv;         

        ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];


        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.ShareType= 'V';
        contentlink.LinkedEntityId = can.Id; 
        contentlink.Visibility = 'AllUsers'; 
        insert contentlink;