• Brian_Kessler
  • NEWBIE
  • 25 Points
  • Member since 2013

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

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?

 

 

 

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?