You need to sign in to do that
Don't have an account?
Brian_Kessler
How can I unit test Uploading a Document using Visualforce and a Custom Controller?
I'm presently playing with the cookbook recipe at http://developer.force.com/cookbook/recipe/uploading-a-document-using-visualforce-and-a-custom-controller
... and trying to figure out how to unit test this properly as I don't know how to do a "mock upload" in SFDC Apex.
I created this test class:
/** * Unit Tests for Uploading a Document using Visualforce and a Custom Controller * @see http://developer.force.com/cookbook/recipe/uploading-a-document-using-visualforce-and-a-custom-controller **/ @isTest private class COOK_UploadDocUsingVFPageControllerTest { public static final User TestRunner = TEST_RunAsUserFactory.create(); private static void doUploadTest(Boolean hasName, Boolean hasBody, Boolean successExpected) { TEST_UniqueHelper uniqueHelper = new TEST_UniqueHelper('Dummy Document'); Document dummyDocument = new Document(); dummyDocument.name = (hasName) ? uniqueHelper.uniqueString : null; dummyDocument.body = (hasBody) ? Blob.valueOf('Unit Test Document Body') : null; ApexPages.Message[] pageMessageArray = null; System.runAs(TestRunner) { Test.startTest(); { COOK_UploadDocUsingVFPageController controller = new COOK_UploadDocUsingVFPageController(); controller.document = dummyDocument; controller.upload(); pageMessageArray = ApexPages.getMessages(); } Test.stopTest(); System.assert (pageMessageArray[0].getSeverity() == (successExpected ? ApexPages.severity.INFO : ApexPages.severity.ERROR)); } } public static testMethod void uploadSuccessTest() { doUploadTest ( true, true, true ); } public static testMethod void noNameUploadFailTest() { doUploadTest ( false, true, false ); } public static testMethod void noBodyUploadFailTest() { doUploadTest ( true, false, false ); } // TODO: Resolve why this test fails! }
but my final testMethod, noBodyUploadFailTest() is itself a failure, resulting in System.AssertException:Assertion Failed.
Any ideas how I should fix this?
While I too expected the condition to throw an exception (which would have been caught and handled by the controller class, resulting in the error message I was looking for), actually the problem was that SFDC does NOT throw an exception if you submit a document without a file, as long as it has a name.
So to make the test work, I needed to go back and modify the controller class to check for a file and add an error if there wasn't one.
All Answers
I thought your code throws a exception.
So this Test method is stoped.
You can add a try catch to your code.
Hope this helps.
Thanks,
Samba
While I too expected the condition to throw an exception (which would have been caught and handled by the controller class, resulting in the error message I was looking for), actually the problem was that SFDC does NOT throw an exception if you submit a document without a file, as long as it has a name.
So to make the test work, I needed to go back and modify the controller class to check for a file and add an error if there wasn't one.