You need to sign in to do that
Don't have an account?

How to write Test Methods for Apex classes?
Hi All,
I want to write test methods for apex classes can any one provide me some usefull links. I created test methods for all my Triggers all are working fine. But i am not getting how to write test methods for apex classes. Plz any one help me.
Thanks,
Aswath.
Hi,
Thanks for ur reply, all ur examples shows how to write test methods for triggers & classes which are calling explicitly in triggers, but in my case i am calling my class on button click.Plz any one help me.
Thanks,
Aswath.
All Answers
The apex language reference has some good info
www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf
http://wiki.apexdevnet.com/index.php/An_Introduction_to_Apex_Code_Test_Methods
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_example.htm
Hi,
Thanks for ur reply, all ur examples shows how to write test methods for triggers & classes which are calling explicitly in triggers, but in my case i am calling my class on button click.Plz any one help me.
Thanks,
Aswath.
Hi there,
I too faced many problems in writing the test cases , later i found out how to write these.
I am still a new bee, so please correct me if I am wrong.
In order to write test method for a button click,
first u need to start with calling your contructor
suppose you are wrting test method for a button click of a standard controller page, for that button clickyou are inserting certain record
object o=new object();
ApexPages.StandardController sc = new ApexPages.StandardController(o);
ClassName cn = new ClassName(sc);
//if you are using any property variable while inserting the record, we just need to assign those properties
cn.property1 = value1;
cn.property2 = value2;
//call the function
cn.function();
//if you are using querystrings then you need to set the current context of the page to accomodate those values
Object u =new Object(field1= value1,field2 = value2,field3=value3);
insert u;
pagereference pg=new pagereference('/apex/VisualForcePage?id='+u.ID);
system.test.setCurrentpage(pg);
object o=new object();
ApexPages.StandardController sc = new ApexPages.StandardController(o);
ClassName cn = new ClassName(sc);
cn.property1 = pg.getparameters().get('id');
cn.function();