function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
IGateIGate 

Reg: Test Class

Help me, to write the test class for below class.

 

 

public with sharing class AttachmentUploadController {
    public String FrameDisplay { get; set; }
    public String Imagesrc { get; set; }
 // Get & Set properties of an Attachment.
  public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
  }
 // Getting Current Id of an Account & Inserting Attached file to an attachment object.
  public pagereference upload() {
    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = ApexPages.CurrentPage().getParameters().get('Id'); // the record the file is attached to
    attachment.IsPrivate = true;
 
    try {
      insert attachment;
      Imagesrc='https://' +ApexPages.Currentpage().getHeaders().get('Host')+ '/servlet/servlet.FileDownload?file=' + attachment.Id;
    }
    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 null;
  }

static testmethod void testm1()
{
    AttachmentUploadController obj=new AttachmentUploadController ();
    obj.upload();
}
}

 

The red marked lines are not covering in my class. How can i cover those red marked lines.

Best Answer chosen by Admin (Salesforce Developers) 
skodisanaskodisana

Hi,

 

Try below.

 

static testmethod void testm1()
{
    AttachmentUploadController obj=new AttachmentUploadController ();
    Account a = new Account(Name='Test Account'); // INSERT all the Required fields

    insert a;

    ApexPages.currentPage().getParameters().put('Id', a.Id);

    obj.upload();
}