-3

i have a vf page which uses input file and simple upload button standard controller is opportunity and extentention is cf

public class cf{
public blob file { get; set; }
private final Opportunity opp;
public cf(ApexPages.StandardController stdController) {    
    this.opp = (Opportunity)stdController.getRecord();  
    }

public PageReference upload() {
    ContentVersion v = new ContentVersion();
    v.versionData = file;  
    v.title = 'dfgdf';
   v.PathOnClient=v.title;
    v.idopp__c=opp.id;
    insert v;
    return new PageReference('/' + v.id); 
}

}

I have problems in writing test class for it below is what i tried

 @isTest
private class Test_cf{

   @isTest
    static void test_upload(){
        test.startTest();
         Opportunity op= new Opportunity();
        op.Name= 'abc';
        op.StageName='Open';
        op.Trattativa__c = 'Automotive';
        op.CloseDate = date.valueOf('2017-11-12');
        op.Tipo_di_Trattativa__c = 'Cross Selling';
        insert op;

         ContentVersion v = new ContentVersion();
         Blob b=Blob.valueOf('Unit Test Attachment Body');
         v.versionData = EncodingUtil.base64Decode('Unit Test Attachment Body');
         v.title = 'dfgdf';
         v.PathOnClient=v.title;
         v.idopp__c=op.id;
         insert v;
         PageReference pageRef = Page.cf;
         Test.setCurrentPage(pageRef);
         pageRef.getParameters().put('id', v.id);



        ApexPages.StandardController std = new ApexPages.StandardController(op);
        cf c = new cf(std);
        c.upload();
        test.stopTest();

    }

}

PageReference pageRef = Page.cf; giving me error page cf doesnt exits from the forum i came to know cf should be the class name for which we are writing testcase.I even tried giving VF page name but no luck.

can some help me out with the test class

Thanks in advance

2 Answers 2

2
PageReference pageRef = Page.cf;

No, Page.cf should be the name of the Visualforce page, not the Apex controller. Change cf to whatever the name of your Visualforce is.

0

Try this code,

 @isTest
 public  class Test_cf{
  static testmethod void test_upload(){
     Opportunity op= new Opportunity();
    op.Name= 'abc';
    op.StageName='Open';
    op.Trattativa__c = 'Automotive';
    op.CloseDate = date.valueOf('2017-11-12');
    op.Tipo_di_Trattativa__c = 'Cross Selling';
    insert op;

      test.startTest();
     PageReference pageRef = Page.yourvfpagename;
      pageRef.getParameters().put('id', string.valueOf(op.id));
     Test.setCurrentPage(pageRef);

   ApexPages.StandardController std1 = new ApexPages.StandardController(op);
    cf c = new cf(std1);
    c.upload();
    test.stopTest();}}
5
  • getting below error System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [VersionData]: [VersionData] but i have populated version data field
    – prasad
    Commented Nov 28, 2017 at 16:37
  • Okay..error itself clears "you are missing required field in version".Itsnt because of page reference.Just run the code snippet in the anonymous window (CTRL+E) for inserting version.You can figure out that issue
    – Annappa PH
    Commented Nov 28, 2017 at 16:38
  • if u can see my question on top , when i call cf.upload() in th etest class it calling upload method in main class where there is a insert statement and it is failing there throwing DML exception saying missing version data filed but here in the test class i have already populated the filed.
    – prasad
    Commented Nov 28, 2017 at 16:43
  • @annppa - it got executed successfully on anonymous window
    – prasad
    Commented Nov 28, 2017 at 16:46
  • Go to this link you will get some idea "salesforce.stackexchange.com/questions/130870/…". I believe page reference issue is resolved. Thanks
    – Annappa PH
    Commented Nov 28, 2017 at 16:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .