[SalesForce] Unit Test For Attachment Upload VF Page

I have a really simple VF page through which a user uploads an attachment to the record of a custom object (launched from a custom button) and I am running into issues trying to construct an appropriate unit test.

This is my VF Page

<apex:page controller="FileUploadController">
  <apex:sectionHeader title="File Upload" subtitle="File Upload"/>

  <apex:form enctype="multipart/form-data">
    <apex:pageMessages id="msgs"/>
    <apex:pageBlock title="Upload a Attachment">

      <apex:pageBlockButtons >
        <apex:commandButton action="{!upload}" value="Save and Complete"/>
      </apex:pageBlockButtons>

      <apex:pageBlockSection showHeader="false" columns="2" id="block1">

           <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
        </apex:pageBlockSectionItem>

      </apex:pageBlockSection>

    </apex:pageBlock>
  </apex:form>
</apex:page>

This is my controller:

public with sharing class FileUploadController {

  public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
  }

  public PageReference upload() {

      id parentid = apexpages.currentPage().getParameters().get('ParentId');

    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = parentid;
    attachment.IsPrivate = true;

    if(!attachment.Name.endsWith('.csv')){
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment. Please convert file to .csv! '));
      return null;
    }

      try{
      Attachment.Body.toString();} catch(StringException e) {ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment. Please encode file in UTF-8 format! '));
      return null;}


    try {
      insert attachment;
        Subscription_Upload__c su = new Subscription_Upload__c(id=parentid);
        su.File_Uploaded__c=true;
        update su;} catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;} finally {attachment = new Attachment();}

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));

    return new Pagereference('/'+ parentid);

  }


}

And here is where I am at so far in my unit test:

@isTest
private class FileUploadControllerTest {


    private static void doUploadTest(){
      Subscription_Upload__c su = new Subscription_Upload__c(Name = 'test');
      insert su;

      PageReference pageref = Page.FileUploadVFPage;
      Test.setCurrentPage(pageref);

      FileUploadController controller = new FileUploadController(su);

      controller = new FileUploadController();

      controller.Attachment.Name = 'unit test Attachment';
      controller.Attachment.Body = Blob.valueOf('unit test Attachment Body');
      controller.uploadFile();

      List<Attachment> attachments = [SELECT ID, Name FROM Attachment WHERE parent.id= :su.id];
      System.assertEquals(1, attachments.size());
    }


}

I am getting a "constructor not defined" error at the moment. I have reviewed, and am trying to follow the documentation provided for custom controllers,but all the examples I have come across do not deal with attachment uploads on the VF pages. I do not have params to pass in, or do I? there is nothing on my page other than a file upload, no other fields exist. Thoughts?

Best Answer

You only have the zero argument default constructor; you're reading the record Id from the page parameters.

ApexPages.currentPage().getParameters().put('ParentId', su.id);
FileUploadController controller = new FileUploadController();

For further reading, see Using Constructors, notably:

A constructor is code that is invoked when an object is created from the class blueprint. You do not need to write a constructor for every class. If a class does not have a user-defined constructor, an implicit, no-argument, public one is used.

Related Topic