1

I have this controller i want to test:

public class SubirCV {
public ContentVersion contentToUpload {get;set;}
public Id memberId{get; set;}
public Blob fileContent {get;set;}
public SubirCV(ApexPages.StandardController controller) {
    memberId = controller.getId();
    contentToUpload = new ContentVersion();
}
public SubirCV(){}
public void uploadContents() {
    List<ContentWorkSpace> CWList = [SELECT Id, Name From ContentWorkspace WHERE Name = 'CVs'];
    contentToUpload.VersionData = fileContent;
    contentToUpload.Candidate__c = memberId;
    insert contentToUpload;

    contentToUpload = [SELECT ContentDocumentId,OwnerId FROM ContentVersion WHERE Id = :contentToUpload.Id];

    ContentWorkspaceDoc cwd = new ContentWorkspaceDoc();
    cwd.ContentDocumentId = contentToUpload.ContentDocumentId;
    cwd.ContentWorkspaceId = CWList.get(0).Id;
    insert cwd;

}

}

I am using a input file tag to upload the document in my visualforce page.

<apex:inputFile fileName="{!contentToUpload.PathOnClient}" value="{!fileContent}"/>

I get an error the in line contentToUpload.VersionData = fileContent

This is my test class

class SubirCVtest {

@isTest static void SubirTest(){
    SubirCV subir = new SubirCV();
    Candidate__c candidate = new Candidato__c(name = 'test',
                                         email__c = 'mail@mail.com',
                                         phone__c = '910000000');
    string cv = 'document';
    Blob cvBlob = Blob.valueOf(cv);

    subir.memberID = candidate.Id;
    subir.fileContent = cvBlob;
    subir.uploadContents();
}

}

The error the system gives me is a null pointer exception. Thanks in advance.

Actualization: I solved it

        insert candidate;
    string cv = 'document';
    Blob cvBlob = Blob.toPdf('document');
    ContentVersion content = new ContentVersion();

    subir.contentToUpload = content;
    subir.contentToUpload.title = 'doc';
    subir.contentToUpload.pathonclient = '/doc.pdf';
    subir.memberID = candidato.Id;
    subir.SetFileContent(cvBlob);
    subir.GetFileContent();
    subir.uploadContents();
  • Can you please share the exact error message & line no. What I see in your test class is you are not inserting "candidate" record but trying to assign it's id : subir.memberID = candidate.Id; May be this could be the reason. – Nebula May 29 '15 at 12:37
  • this is the error: System.NullPointerException: Attempt to de-reference a null ||||||| these are the lines: object Class.SubirCV.uploadContents: line 12, column 1 Class.SubirCVtest.SubirTest: line 15, column 1 ||||||| line 12 of the controller is: contentToUpload.VersionData = fileContent; |||| line 15 of the test is: subir.uploadContents(); – Jose Gallego May 30 '15 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.