You need to sign in to do that
Don't have an account?

File Attachment Unit Test Help
I am creating unit tests for a custom controller I created to handle file uploads and am having issues populating the Attachment body property in my test method. I really just don't know how I'm supposed to simulate a file upload during unit testing.
The body property is normally populated from the visualforce apex:inputFile tag, where I bind the inputFile to the attachment body property in my controller.
<apex:inputFile value="{!file.body}" />
I know that the Attachment body must be encoded in Base64, but how do I create a variable in my test class that I can use for filesize bounds testing as well? Can anyone provide some direction or even code samples on how I would go about doing this?
My test method is as follows:
static testMethod void basicControllerTest()
{
System.debug('Unit Test: AttachOppPaperwork Controller');
//Create Opportunity record
Opportunity o = new Opportunity();
o.name = 'Test Opp 1';
o.stageName = 'First Appointment';
o.closeDate = system.today()+60;
insert o;
//Set current page to AttachOppPaperwork
PageReference oPage = new PageReference('/apex/AttachOppPaperwork?id='+o.Id);
Test.setCurrentPage(oPage);
//Instantiate Controller
AttachOppPaperworkController c = new AttachOppPaperworkController();
//Verify variable values
System.assertEquals(o.name, c.oppName); //c.oppName == [Opportunity Name]
System.assertEquals(o.id, c.oppId); //c.oppId == [Opportunity Id]
System.assertNotEquals(null, c.file); //c.file instantiated
//Test File Upload
System.debug('-->AttachOppPaperworkController.upload() called');
//Set file body
//???
//upload the attachment
PageReference uploadTest = c.upload();
System.debug('::AttachOppPaperworkController.attachmentId = ' + c.attachmentId);
System.assertNotEquals(null, c.attachmentId); //attachmentId property should not be null
System.assertEquals(null, uploadTest); //method should return null, refreshing the page
System.assertEquals(true, c.complete); //complete flag should be set to true
//Verify Opportunity field update
Opportunity oTest = [SELECT PaperworkUploadedDate__c FROM Opportunity WHERE Id=:o.Id];
System.debug('::PaperworkUploadedDate__c = ' + oTest.PaperworkUploadedDate__c);
System.assertNotEquals(null, oTest.PaperworkUploadedDate__c); //date field should have a value
}
Here's how I fill out the body for Unit Testing:
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); controller.attachment.body=bodyBlob;
Dear bob,
if I push this control I got a failure in my test runner with "attempt to do-reference a null object":
this is my code
can you help me please?
thanks for any suggest.
kind regards,
Maurizio
What does your debug statement on the attachment body output?
ok fine, the code has always right ;-)
this is the problem:
but now I have one more question:
how can I test this condition?
maybe I can create an attachment with a size more 2mb ... (i'm not sure)
and the next one, how can I test the catch (DMLException e) ?
thanks very much for your quickly response.
kind regards,
Maurizio
I'm not sure if there's any restriction on the size of the strings that are used to create the attachment body - you could try to keep appending the characters. If not, you may need to refactor the max size out to a method and change it dynamically based on the Test.IsRunningTest return value.
Rather than catching the DML exception, I'd use Database.insert which returns a structured value - its much easier to parse in code.
what wonderful idea, it's working fine. I prefer the second one, to change the vLimitAttachSize during the test, because I'm not sure that I can create a 2mb of attach, too.
sorry, can you explain what do you mean with:
Thanks again for you precious support.
Sure - where you have DML operations like insert/update/delete, there are equivalents on the standard Database class.
So where you might insert a record using:
you can also use:
You can then interrogate the SaveResult to see what happened in terms of success/fail and reasons.
and with this class the pagereference test work with success (path try {}). How can I test the catch, please? (path catch{}).
thanks for your time and fix my problem.
Ah, sorry - I thought you were asking how to catch the errors and process them.
Unfortunately this is one of the downsides of developing in Apex - its difficult to engineer exceptions as you can't tweak the behaviour further down the line.
However, in this case it should be possible if you create another test and don't specify the name for the attachment - as that is a required field your insert will fail.
No problem - glad to hear its working.