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
Brian_KesslerBrian_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?

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Brian_KesslerBrian_Kessler
Thanks for the feedback, but I got the answer off Stackoverflow < http://salesforce.stackexchange.com/questions/15736/how-can-i-unit-test-uploading-a-document-using-visualforce-and-a-custom-controll/15743?noredirect=1#comment22811_15743 >.

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

sambasamba

I thought your code throws a exception.

 

COOK_UploadDocUsingVFPageController controller = new COOK_UploadDocUsingVFPageController();
            										controller.document = dummyDocument;
													controller.upload();

 So this Test method is stoped.

 

You can add a try catch to your code.

 

Hope this helps.

 

Thanks,

Samba

Brian_KesslerBrian_Kessler
Thanks for the feedback, but I got the answer off Stackoverflow < http://salesforce.stackexchange.com/questions/15736/how-can-i-unit-test-uploading-a-document-using-visualforce-and-a-custom-controll/15743?noredirect=1#comment22811_15743 >.

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.
This was selected as the best answer