0

I have built a Visualforce page that allows the user to quickly choose a file and attach it to the record without having to go to an additional page. I have been attempting to build a test class for the controller. I have been able to do successful Tests, but there has been 0% code coverage. Can anyone please help me understand what I'm missing?

Controller:

public with sharing class AttachmentUploadControllerDemo {

  public String attachmentName {get; set;} 
  public Blob attachmentBody {get; set;}

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

  public  Apttus__APTS_Agreement__c agmt = new Apttus__APTS_Agreement__c();
  public  Apttus__APTS_Agreement__c agmt1 = new Apttus__APTS_Agreement__c();
  public String redirectUrl {public get; private set;}
  public Boolean shouldRedirect {public get; private set;}
  public Boolean attached {public get; private set;}
  public Boolean notAttached {public get; private set;}
  public Boolean preAttached {public get; private set;}
  public Boolean lpoOwned {public get; private set;}
  public Boolean lpoAttached {public get; private set;}
  ID agmtid;
  String own;

  List<Id> listIds = new List<Id>();  
  List<Apttus__APTS_Agreement__Share> agreementShares = new List<Apttus__APTS_Agreement__Share>();



  String attachedReturn = Apexpages.currentPage().getParameters().get('attachedReturn');

  public AttachmentUploadControllerDemo(ApexPages.StandardController Controller) {  
      this.agmt = (Apttus__APTS_Agreement__c)Controller.getRecord();
      shouldRedirect = false;
      agmtid = agmt.id;
      agmt1 = [Select ID, Owner.Name From Apttus__APTS_Agreement__c Where ID=:agmtid LIMIT 1];
      own = agmt1.owner.name;
      if(own=='LPO Storage'){
          preAttached=false;
          lpoOwned = true;
      }
      else{
          preAttached=true;
          lpoOwned = false;
      }

  }

    public PageReference unAttach(){
        notAttached = false;
        preAttached=false;
        lpoAttached=false;
        return null;
    }  

  public PageReference upload() {


    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = agmt.id; // the record the file is attached to
    attachment.IsPrivate = true;
    attachment.name = attachmentName;
    attachment.body = attachmentBody;
    attachment.isPrivate = false;

    if(attachmentName == null){
        notAttached = true;
        attached = false;
        preAttached=false;
        lpoOwned=false;
        lpoAttached=false;
        return null;
    }
      else{
          try {
            insert attachment;
                if(own=='LPO Storage'){
                  lpoAttached=true;
                  attached=false;
                }
                else{
                  attached = true; 
                  lpoAttached=false;
                }                   
                notAttached = false;
                preAttached=false;
                lpoOwned=false;
            } catch (DMLException e) {
                attached = false;
                notAttached = true;
                preAttached=false;
                lpoOwned=false;
                lpoAttached=false;
                return null;
            } finally {
                attachment = new Attachment(); 
            }
      }


    shouldRedirect = true;
    redirectUrl = '/' + agmt.id;
    return null;
//      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));

}
}

Test Class:

@isTest
public class AttachmentUploadController_Test {

    private static  testMethod void AttachmentUploadController_Test()
    {

        Profile p = [select id from profile where name='Standard User'];
        User u = new User(alias = 'standt', email='standarduser6@testorg.com',
            emailencodingkey='UTF-8', firstname='fake', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id,
            timezonesidkey='America/Los_Angeles', username='standarduser6@testorg.com');
        insert u;

        Apttus__APTS_Agreement__c agmt1 = new Apttus__APTS_Agreement__c();        
        agmt1.Ownerid = u.id;
        insert agmt1;

        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');

        test.starttest();

        PageReference pgRef = Page.Attach_File_Upload;
        Test.setCurrentPageReference (pgRef);
        ApexPages.currentPage().getParameters().put('Id',agmt1.id);

        AttachmentUploadControllerDemo attch = new AttachmentUploadControllerDemo(new ApexPages.StandardController(agmt1));  
        System.assertEquals(true, attch.preattached);
        attch.unAttach();
        attch.attachmentName = 'Test';
        attch.attachmentBody = bodyBlob;
        attch.upload();
        test.stopTest();
    }

}

1 Answer 1

2

Checking the "Always Run Asynchronously" box will fix your test coverage count issue

enter image description here

0

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.