+ Start a Discussion
DevelopersDevelopers 

How to write the test methods for Webservices callouts?

Hi Friends,

 

Please give me the information, how to write the test methods for Webservices callouts.

 

Regards,

Phanikumar

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

 

public class TestWSDL

{

 

    public boolean isTestExcution{get;set;}

    public TestWSDL(){isTestExcution=true;}

    public TestWSDL(boolean var)

    {

      

        isTestExcution=var;

    }

   

    public void CallService()

    {

    try

    {

   

    System.debug('---------------------obj.Addition-------------------'+UserInfo.getSessionId());

    CheckWebservice.Plan  obj=new CheckWebservice.Plan();

   

         partnerSoapSforceCom.Soap sf = new partnerSoapSforceCom.Soap();

      sf.endpoint_x = 'https://login.salesforce.com/services/Soap/u/24.0';

       partnerSoapSforceCom.LoginResult LR;

     if(isTestExcution!=null && isTestExcution)

     {

     

        LR = sf.login('vg@ymail.com','ac3333321323VfNzDJrKcBqF4L');

      }

         System.debug('---------------------LR -------------------'+LR);

            CheckWebservice.SessionHeader_element session= new  CheckWebservice.SessionHeader_element();

             session.sessionId = LR.sessionId;

            

             System.debug('---------------------session-------------------'+session);

             obj.SessionHeader=session;

            

           

             integer i=obj.Addition(25,5); 

             //Integer i=obj.secondnumber;

         System.debug('---------------------Sum-------------------'+i);

        

         }

         catch(Exception e)

         {

             System.debug('---------------------Exception -------------------'+e);

         }

        

 

    }

   

   

    public static testMethod void TestWSDL()

    {

    try

        { 

       

       

           TestWSDL obj=new TestWSDL(false);

          

                //obj.isTestExcution=false;

                obj.CallService();

               

              // integer k= obj1.Addition(9,11);    

                   

         }

        

        

         catch(Exception e)

         {

             throw e;

         }

      }  

 

}

 

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.