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
Michael Hedrick 2Michael Hedrick 2 

Methods defined as TestMethod do not support Web service callouts but not during testing

Hello All,
I have a Class that performs a call out;
Global class CallTrexWebServiceAccountUpdate{
    
    public static Account accounts {get;set;}
    public static String myresponse{get;set;}

    webservice static string ABCWebServiceAccountUpdate(String id) {
    
        ABCWebServiceAccountUpdateAsync(id);
        return myresponse;        
    }


    @future(Callout=true)
      static void ABCWebServiceAccountUpdateAsync(String id)  
       {
       System.debug(id); 
           accounts = [SELECT id,Partner_Type__c FROM Account WHERE id =:id];
             if(id != null )
               {
                   HttpRequest req = new HttpRequest();
                   Http http = new Http();
                   req.setMethod('GET');
                            
                   String url = 'https://blabla/Services.asmx/UpdateAccount?accountId='+id;
                   req.setEndpoint(url);
            
                   HTTPResponse resp = http.send(req);
                   myresponse = resp.getBody();
             }
       }
    }

Have a test Class and this works fine.


I now have additional Apex triggers to deploy and now  its failing on the above Class.  The trigger I am deploying is related to the Cases.

The fatal error is on line :   HTTPResponse resp = http.send(req);
Methods defined as TestMethod do not support Web service callouts

And this code did deploy to production originally but I deployed with just the Class and test Class.

If anyone can spot the issue or has any suggestions I would greatly appreciate it.

Regards,
M
Best Answer chosen by Michael Hedrick 2
Raj VakatiRaj Vakati
Global class CallTrexWebServiceAccountUpdate{
    
    public static Account accounts {get;set;}
    public static String myresponse{get;set;}

    webservice static string ABCWebServiceAccountUpdate(String id) {
    if(!Test.isRunningTest()){
        ABCWebServiceAccountUpdateAsync(id);
        return myresponse;   
}	else{

return 'Dummry Resp' ;
}	
    }


    @future(Callout=true)
      static void ABCWebServiceAccountUpdateAsync(String id)  
       {
       System.debug(id); 
           accounts = [SELECT id,Partner_Type__c FROM Account WHERE id =:id];
             if(id != null )
               {
                   HttpRequest req = new HttpRequest();
                   Http http = new Http();
                   req.setMethod('GET');
                            
                   String url = 'https://blabla/Services.asmx/UpdateAccount?accountId='+id;
                   req.setEndpoint(url);
            
                   HTTPResponse resp = http.send(req);
                   myresponse = resp.getBody();
             }
       }
    }

 

All Answers

Raj VakatiRaj Vakati
Global class CallTrexWebServiceAccountUpdate{
    
    public static Account accounts {get;set;}
    public static String myresponse{get;set;}

    webservice static string ABCWebServiceAccountUpdate(String id) {
    if(!Test.isRunningTest()){
        ABCWebServiceAccountUpdateAsync(id);
        return myresponse;   
}	else{

return 'Dummry Resp' ;
}	
    }


    @future(Callout=true)
      static void ABCWebServiceAccountUpdateAsync(String id)  
       {
       System.debug(id); 
           accounts = [SELECT id,Partner_Type__c FROM Account WHERE id =:id];
             if(id != null )
               {
                   HttpRequest req = new HttpRequest();
                   Http http = new Http();
                   req.setMethod('GET');
                            
                   String url = 'https://blabla/Services.asmx/UpdateAccount?accountId='+id;
                   req.setEndpoint(url);
            
                   HTTPResponse resp = http.send(req);
                   myresponse = resp.getBody();
             }
       }
    }

 
This was selected as the best answer
Raj VakatiRaj Vakati
Use the above code 
Michael Hedrick 2Michael Hedrick 2
That is spot on Raj. 
Should I always consider using the if(!Test.isRunningTest()) syntax in classes that perfomr a Call out?
If not, why did you receommend using it in this scenerio?

Thanks,
M


 
Raj VakatiRaj Vakati
When you insert data in the test class, which will cause the trigger to fires . In your case, your trigger contains web service code which is executing from the test class which is not correct .. 

Please refer this article

. Testing callouts - in your callout code you check to see if you're executing within a unit test context by checking Test.isRunningTest() and instead of getting your callout response from an HttpResponse.send() request, you return a pre-built test string instead. 
 

https://help.salesforce.com/articleView?id=000205831&type=1


Mark it as solved!
 
Michael Hedrick 2Michael Hedrick 2
Hey Raj,
So are you saying I should have split the test class into 2 parts?
In your case, your trigger contains web service code which is executing from the test class which is not correct .

M