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
DipikaDipika 

test webservice api callouts

hello,

          I m using google service class for authenticating user on my page. but i m using direct google service class methods. so when i m creating test class it is giving error of webservice callout. so is there any way to use google service class and also test that class in which i m using this??

cloudmaniacloudmania

You cant not invoke a web service in your test class.Try to skip that line by using some boolean variable.It is such a hack like this>

Public class WebSrvcClass

{

public boolean Istest{get;set;}

public WebSrvcClass()

       {

           Istest=false;

       }

      public void urwebservicemethod()

    {

         .........

        if(!Istest)

        HttpREquest.send(.....);

     }

    private static void testmethod urmethod()

    {

        public WebSrvcClass class=new WebSrvcClass();

      class.isTest=true;

     }

}

LakshmanLakshman

We have system static method to do this so we dont need a seperate variable to do this.

So modified class should be like:

 

Public class WebSrvcClass
{
public WebSrvcClass()
{
        //Some logic to be invoked at start
}
public void urwebservicemethod()
{
         .........
        if(!Test.isRunningTest())
        HttpREquest.send(.....);
}

private static void testmethod urmethod()
{
        public WebSrvcClass class=new WebSrvcClass();
class.urwebservicemethod();
} }

 BTW you can create a fake request in your class and try calling a method which accepts required parameters for HttpRequest. You should use this method in your main class as well to send HTTP request.

 

Regards,

Lakshman