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
AswathAswath 

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.

Best Answer chosen by Admin (Salesforce Developers) 
AswathAswath

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

dev_forcedev_force

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

AswathAswath

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.

This was selected as the best answer
yvk431yvk431

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();

 

Message Edited by yvk431 on 12-28-2009 08:57 PM
JMPlayer22JMPlayer22
THIS IS AWESOME!!! Thank you SO much for posting this.  I dunno what the deal is, but it seems like Salesforce covers all thing things that DON'T apply to what I need to do as far as the developer guides.  This is EXACTLY what I've been looking for.