0

I'm trying to write a unit test for a method in a class I wrote that attaches a file to an event record.

I want to test that if the attachment does not insert give an error, else assert that the blob of the body is correct.

Here is a snip of the class:

    public PageReference upload() {

attachment.OwnerId = UserInfo.getUserId();
attachment.ParentId = Event.Id;
attachment.IsPrivate = false;

try {
    insert attachment;
} 
catch (DMLException e) {
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
    return null;
} 
finally {
    attachment.body = null;  
    attachment = new Attachment(); 
}

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

Here is the test class:

@isTest static void addAttachment(){
    // Get the new Account Id we created and put it into the SL_CllReport vf page.
    Account acc = [Select Id FROM Account];

    // Grab the Event that was created. 
    Event event = [Select Id FROM Event];   

    // Grab the User that was created.
    User u = [Select Id FROM User WHERE UserName = 'apextestuser@npd.com']; 



    PageReference objPg = Page.SL_CallReportEdit;
    Test.setCurrentPage(objPg);
    System.currentPageReference().getParameters().put('Id', event.Id);
    ApexPages.standardController stdController = new ApexPages.standardController(event);
    SL_CallReportEditController callReport = new SL_CallReportEditController(stdController);   

    callReport.attachment.OwnerId = u.Id;
    callReport.attachment.ParentId = Event.Id;
    callReport.attachment.IsPrivate = false;
    callReport.attachment.Name = 'Test Attachment';
    callReport.attachment.Body = Blob.valueOf('Dan Dan Dan');

    callReport.upload();

    // Verify that the attachment was created
    List<Attachment> att = [Select Id, Body FROM Attachment WHERE ParentId = :Event.Id];

    if(att[0].Id == null){
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
    }
    else{
    System.debug('Body is: ' + att[0].Body);
    system.assertEquals(Blob.valueOf('Dan Dan Dan'), att[0].Body);
    }
}

Am I writing this right? When I run the test I get an error: System.ListException: List index out of bounds: 0 on my if(att[0].Id line.

How can I try to insert an attachment and then verify that it attached to the record?

1

If the attachment was not inserted, there is nothing to query back from the database, so att.isEmpty() will be true. There's no reason you should be using ApexPages.addMessage() in your unit test, however, because unit tests don't display messages. More to the point, you should expect your code to either work or not work; that's the point of a unit test.

Your unit test should look more like this:

Attachment att = [Select Id, Body FROM Attachment WHERE ParentId = :Event.Id];
System.assertEquals(Blob.valueOf('Dan Dan Dan'), att.Body);

You'll need to debug why your code is failing; if the query returns no results, that means that your insert failed, so you'll want to check your debug logs. There's a few other issues with your code as well, so you'll want to do some research to see how you can improve both your unit tests and live code.

| improve this answer | |
  • Okay, it seems strange in that the live code works, I can be on the page add an attachment and it saves to the record. I'll take another look see where I'm going wrong. – Dan Wooding Jul 22 '16 at 19:12
  • any other hints on where the code has issues? I'm not able to solve this. – Dan Wooding Jul 22 '16 at 20:24

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.