+ Start a Discussion
TechSteveTechSteve 

constructor error in test class

HI, New to this so treat me as dumb. Writing a unit test class for a very small class but keep getting this message

Error: Compile Error: Constructor not defined: [attachmentsample].<Constructor>()  yet I used the same format in another test and it doesn't complain! what is missing? cant see the wood for the tree's i think.

** class **

public class attachmentsample {

    public attachmentsample(ApexPages.StandardController controller) {

    }
    Public Attachment myfile;
    Public Attachment getmyfile()
    {
        myfile = new Attachment();
        return myfile;
    }
  
    Public Pagereference Savedoc()
    {
        String accid = System.currentPagereference().getParameters().get('id');

        Attachment a = new Attachment(parentId = accid, name=myfile.name, body = myfile.body);
        
         /* insert the attachment */
         insert a;
        return NULL;
    }  

}

** controller **

public class attachmentsampleTest {

    public static void attachmentsampleTest(ApexPages.StandardController controller) {
        PageReference pageRef = Page.attachment;
        test.setCurrentPage(pageRef);
        ApexPages.CurrentPage().getParameters().put('id', '003R000000Y7Gw4');

        attachmentsample atsam = new attachmentsample();
       
        controller.myfile.name = 'testdoc';
        controller.myfile.body = 'testing 123';
      
    }
}

Please I am really under pressure to get this stuff working but keep going round in circles.

Thanks

Steve.

Devendra@SFDCDevendra@SFDC

 

Hi Steve,

 

Try this:

 

Make following changes in saveDoc method and test method :

public pagereference Savedoc()
    {
   
      String accId=System.currentPagereference().getParameters().get('id'); //Retreive JobId
      if(Test.isRunningTest())
        {
            myfile.body = Blob.valueOf('Test Attachmant 613');
            myfile.Name = 'TestName 613';
        }
      
      Attachment objAttachment=new Attachment(parentId=accId,name=myfile.Name,body=myfile.body); //Create instance of Attachment and add fields to it
      
      /* Insert the Attachment*/
     
          insert objAttachment;
      }

/* Test Method */
static testMethod void myAttachment()
        {
Account acc=new Account(Name=’test account 123’, --insert all required fields--); insert acc;
ApexPages.StandardController con=new ApexPages.StandardController(acc);
                   
               ApexPages.currentPage().getParameters().put('Id', acc.Id);
               
Attachmentsample ext= new attachmentsample (con);
               
               test.startTest();
               
               ext.getmyfile();
               
               Pagereference pg=ext.SaveDoc();
               
               test.stopTest();   
}
/* End Test Method */
   

 

Hope it helps.

 

Regards,

Devendra S

TechSteveTechSteve

Thanks Devindra But getting Compile Error: Method does not exist or incorrect signature: [attachmentsample].SaveDoc() when saving the test code and Compile Error: Non-void method might not return a value or might have statement after a return statement. in the controller.

 

any ideas?

 

Steve

Devendra@SFDCDevendra@SFDC

 

Hi Steve,

 

Can you please send your updated code?

 

Regards,

Devendra