+ Start a Discussion

How to write Test Method for Webservice Method (apex class)

Hi all!!


I have  aparticular piece of apex code that is excuted on click of a button (Javascript based custom button).. I am facing problem in writing test mthods for it..


Callout from button is like:-



var result=sforce.apex.execute(
"calculateData", // class
"initialMethod", // method





The apex class calculateData is like :-


global class calculateData {


Webservice static String initialMethod()  { 


    //Variables Declaration 
    /* SOQL query to 2 different objects storing raw data and then calulation    logic applied(call to calc method) and calculated data inserted to a 3rd object



return 'success'; 




 /*Method for Calculation */
  public Double doCalc(arguments passed from initialMethod)

     // calculation logic






Now, I am not able to write test methods for this class. I am really confused about what dataset should I create for testing, as the data is fetched from the existing objects!


Please help!! I hope the scenario is clear.. i have tried my best to put my problem across.. Any help or suggestion?


In test method it doesn’t matter that data is from existing object or hardcoaded, just call methods of class, pass the appropriate values to methods and cover the code as much as possible.