You need to sign in to do that
Don't have an account?
Ryan Greene
test apexpages.standardcontroller
Hello,
I have not written Apex Tests before (I probably should have) and I am running into an error each time I run the test, but there is no error on the page. Test Class below and I am receiving the error on line 6: Error Message - System.NullPointerException: Attempt to de-reference a null object Stack Trace - Class.Test_UploadAttachmentController.testattach
I have not written Apex Tests before (I probably should have) and I am running into an error each time I run the test, but there is no error on the page. Test Class below and I am receiving the error on line 6: Error Message - System.NullPointerException: Attempt to de-reference a null object Stack Trace - Class.Test_UploadAttachmentController.testattach
@isTest private class Test_UploadAttachmentController { static testMethod void testattach(){ Document__c doc = new Document__c(Document__c = 'Aging', RecordTypeId = [SELECT Id, SobjectType, Name FROM Recordtype WHERE Name = 'Request' AND SobjectType = 'Document__c' LIMIT 1].Id); insert doc; UploadAttachmentController controller = new UploadAttachmentController(new ApexPages.StandardController(doc)); controller.fileName = 'Unit Test Attachment Body'; controller.fileBody = Blob.valueOf('Unit Test Attachment Body'); controller.processUpload(); LIST<Attachment> attch = [SELECT Id, Name FROM Attachment WHERE parent.id =: doc.Id]; System.assertEquals(1, attch.size()); } }
PageReference myVfPage = Page.MyVFPName;
Test.setCurrentPage(myVfPage); // Put Id into the current page Parameters
ApexPages.currentPage().getParameters().put('id',doc.id);
UploadAttachmentController controller = new UploadAttachmentController(newApexPages.StandardController(doc));
All Answers
insert doc; seems to be working but the controller could need other initialiazed values and we cannot guess them.
System.NullPointerException: Attempt to de-reference a null object : the controller wants to use a variable but it is not initialized
Very common:
List<MyObject> myList; without myList = new List<MyObject>(); for instance
// use of myList => error System.NullPointerException
or
MyObject myobj = [SELECT with a request returning nothing so null];
myobj.myfield; // error
Regards
Please help with the new error I am receiving!
The new error is line 15 of the Test Class: Error Message System.AssertException: Assertion Failed: Expected: 1, Actual: 0
Stack Trace Class.Test_UploadAttachmentController.testattach: line 15, column 1
So an Attachment is not being set through the test
I'm at 65% code coverage with this test, so any other tips would be much appreciated!
ApexPages.currentPage().getParameters().put('parameter', 'value');
ApexPages.currentPage().getParameters().put('id', doc.id);
Not the complete solution that is important for your test.
PageReference myVfPage = Page.MyVFPName;
Test.setCurrentPage(myVfPage); // Put Id into the current page Parameters
ApexPages.currentPage().getParameters().put('id',doc.id);
UploadAttachmentController controller = new UploadAttachmentController(newApexPages.StandardController(doc));
Forgot to add in my VF Page for testing, your suggestions worked perfectly!