I have a upload feature that takes a file from user(only pdf allowed) on visualforce page and then insert it in notes and attachment section of a record.My code works perfectly fine. However, I don't know how to test the code. Please help. Here is my controller and visualforce class.

Visualforce code:

 <div class="slds-grid slds-gutters slds-grid_vertical-align-end slds-wrap">

 <div class="slds-col slds-size_1-of-1 slds-large-size_1-of-1 slds-p- 

 <apex:pageBlock >

       <apex:pageBlockSection >

              <apex:outputLabel >Select your file</apex:outputLabel>

              <apex:inputFile onchange="check(this)" id="myFile" value=" 
              {!myDoc.Body}" fileName="{!myDoc.name}" 


       <apex:commandButton id="uploadfile" value="Upload" action=" 




Here is the javascript code:


function check(obj) {

   var path = obj.value;

   var ext = path.substring(path.lastIndexOf('.') + 1);

   if(ext !="pdf")


       obj.value = null;

       window.alert("Please select only pdf");

       return false;



Here is my controller class:

public Boolean uploadletterofrecommendation {get; set;}
private Transient Attachment myDocument;
public Transient Attachment myDoc






            myDocument = new Attachment();


        return myDocument;




public PageReference upload()


         myDoc.parentId = recommendation.Id;

         insert myDoc;
         String message = 'File has been uploaded successfully';
         ApexPages.Message(ApexPages.Severity.CONFIRM, message));

         return null;


Honestly you don't have to do much here. Most of this code doesn't require data setup really so you just have to reference and set some of the variables in the test class and call your methods. Especially since your myDoc variable here is self-setting when the main Document is of a null value.

Try putting together a simple test class where you call the methods and set what you can in the class and you should be able to get decent coverage just from that since all you're looking at is the controller itself.

  • Thanks for replying. I did this after calling the constructor and it worked for me.. controllerClass.myDoc.name='Unit Test Attachment'; controllerClass.myDoc.Body=Blob.valueOf('Unit Test Attachment Body'); controllerClass.upload(); – Student Jun 28 '18 at 22:56
  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review – glls Jun 29 '18 at 0:22
  • @glls I understand what you are saying. However in this instance I answered the question by pointing him in the right direction rather than flat out writing his code for him because the solution was simple, and his comment was not asking for clarification so I'm not entirely seeing where you got that from. – Thuesmann Jun 29 '18 at 12:34
  • your answers does not contain any resources to point the user in the right direction, hence the "more suited as a comment" , if you'd like to review an example of a "Good Answer" to this type of qeustion, take a moment to read how-to-increase-code-coverage-of-a-test-class. Maybe the user is new to unit testing, pointing towards a Trailhead module can be beneficial to the user or Salesforce documentaiton. – glls Jun 29 '18 at 13:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.