4

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?

1

Assertion failed as the Page didn't return message with severity 'ERROR'.
Check if your page is returning message with severity 'ERROR' for the specified condition.

I hope you are setting up test context for your page and passing required parameters and then invoking controller methods.
If not you can set up test context using this code:

PageReference pageRef = Page.YOUR_PAGE_NAME; <br/>
Test.setCurrentPage(pageRef); <br/>
//Setting required parameters <br/>
ApexPages.currentPage().getParameters().put('id', testObj.Id);
| improve this answer | |
  • Just checked... You are right: the page itself doesn't return error if I submit without actually including a file for the document (I would have expected that to be required, but didn't check).... that said, SFDC seems happy to run my tests on the controller without my specifying any page reference (as above).... and testObj.id is presently an invalid reference. Can you elaborate why you feel this is needed? – Brian Kessler Aug 21 '13 at 15:51
  • 1
    Yes, you can unit test Controller/Extension classes by just instantiating respective classes and calling methods in controller/extension without setting any Page (current page) as test context. Just to make sure required parameters are passed and to mimic a Visualforce page load, you have to set the test context for the page you are testing. – zuke Aug 22 '13 at 5:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.