3

Typical of custom controller extensions, my extension has a signature

public MyVFCtrlExt (ApexPages.StandardController standardController)

I'm currently trying to figure out how to write unit tests for the methods in the controller class.

To do this, I want to create an instance of MyVFCtrlExt, so I can write tests like this:

@isTest private static void testMyMethod()
{
     // GIVEN
         /* Some setup code */

     ApexPages.StandardController standardController = null; // TODO;
     MyVFCtrlExt myVFCtrlExt = new MyVFCtrlExt(standardController);

     // WHEN
     System.runAs(TEST_RUN_USER)
     {
        Test.startTest();
        {
            myVFCtrlExt.myMethod();
        }
        Test.stopTest(); 
     }

     // THEN
         /* Assert some things. */

}

Unfortunately, I can't figure out how to create an instance of the ApexPages.StandardController, which is a prerequisite to creating the controller extension.

How can/should I do this?

4

You will need to first provide object reference and then use that as a constructor, please see below example:

ex=TestDataFactory.createexam(tform.Id,extaker.id,NEWx);
ex.Name='Chemistry';
ex.Test_Form__r=tform;
ex.Test_Form__r.Passing_Score__c=tform.Passing_Score__c;
insert ex

// call the constructor
Examcontroller  controller = new Examcontroller (new ApexPages.StandardController(ex));
  • This looks good! Will try when I get back to work tomorrow. Thanks. :-) – Brian Kessler Nov 17 '14 at 18:10

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.