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
David.HEDavid.HE 

How to test a Page with controller extension

As you can see in this question, I am new in Test Classes.

 

i have a page wich have 3 opportunity fields,and these fields can be modified.

 

In the test class I need to instanciate the controller and  assign a new value in the opportunity name field.

 

In the controller, I get the opportunity querying to the database using the Id of the Opportunity that I get through Get parameter.

 

Can you help me in achieve it? (I suppose that it should be very easy, but after a long search in internet, I haven't found it)

Best Answer chosen by Admin (Salesforce Developers) 
Puja_mfsiPuja_mfsi

HI,

Suppose your class name is 'OpportunityController'

Then first you need to create an opportunity record in your test class,because your test class can't access data from salesfore database unless we don't put (seeAllData=true) .

 

Opportunity oppty = new Opportunity();

oppty.closeDate('');

//Put all the mandatory field value or other field value which u want.

insert oppty.

 

And pass this opprtunity id to your parameter.As:

apexpages.currentpage().getparameters().set('id',oppty.Id);   // where Id is the name of the parameter.

 

After that instantiate your contoller.

OpportunityController opptyObj = new OpportunityController();

 

OR if your class constructor uses opportunity as a standard controller then :

 

ApexPages.StandardController sc = new ApexPages.StandardController(oppty);
OpportunityController opptyObj = new OpportunityController(sc);

 

 

Please take reference from the below URLs:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_example.htm

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm

 

 

Please let me know if u have any problem on same and if this post helps u please give KUDOS by click on star at left.

 

 

All Answers

Puja_mfsiPuja_mfsi

HI,

Suppose your class name is 'OpportunityController'

Then first you need to create an opportunity record in your test class,because your test class can't access data from salesfore database unless we don't put (seeAllData=true) .

 

Opportunity oppty = new Opportunity();

oppty.closeDate('');

//Put all the mandatory field value or other field value which u want.

insert oppty.

 

And pass this opprtunity id to your parameter.As:

apexpages.currentpage().getparameters().set('id',oppty.Id);   // where Id is the name of the parameter.

 

After that instantiate your contoller.

OpportunityController opptyObj = new OpportunityController();

 

OR if your class constructor uses opportunity as a standard controller then :

 

ApexPages.StandardController sc = new ApexPages.StandardController(oppty);
OpportunityController opptyObj = new OpportunityController(sc);

 

 

Please take reference from the below URLs:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_example.htm

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm

 

 

Please let me know if u have any problem on same and if this post helps u please give KUDOS by click on star at left.

 

 

This was selected as the best answer
David.HEDavid.HE
Thank you very much for your broad reply.
Puja_mfsiPuja_mfsi

HI,

Please throw KUDOS if this post helps you. :)