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
Aditya Singh 37Aditya Singh 37 

Apex Test Class for Future callOut

Hi, 
 I Created a class for Future callout and trying to create a Test Class for the same. I am invoking this class through an invokeable Apex class which gets called via a process builder. I tested this in Sandbox and it works pefect but i am not sure how to write the test class and migrate it. Below is the entire code, i am not sure , i am wrighting the right code ?

=====================================================================================
public class ApexCallout { // Called Through ProcessBuilder
@InvocableMethod
public static void invokecallout(list<Lead> Leads) {
WS_Webservice.Notification(Leads[0].id, Leads[0].name)
   }
}
------------------------------------------------------------------
@isTest
private class ApexCalloutTest{
    private static testMethod void doTest() {

        Test.startTest();
        Lead l = new Lead(id = '00XXXXXXXXXXXXXX',
                          name = 'User123');
        Test.stopTest();

    }
}


======================================================================================
Global class WS_Webservice {
  @future (callout=true)
   // Receiving the Lead details from Invokable class ApexCallout.
  WebService static void Notification(id lid, String name)
        { 
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();

        req.setEndpoint('https://google.com');
        
        //Setting Method Content and Body
        req.setMethod('POST');
        req.setHeader('Content-Type','application/x-www-form-urlencoded');
        req.setBody(
        'name='+EncodingUtil.urlEncode(name, 'UTF-8')
        
      );
        req.setTimeout(120000);
        try {
         res = http.send(req);
         System.debug(res.getbody());
        
         Dom.Document docx = new Dom.Document();
         docx.load(res.getbody());
         dom.XmlNode xroot = docx.getrootelement();
         String U_Id = xroot.getAttributeValue('U_Id', null);
         system.debug(U_Id );
         Lead le = new Lead(id=lid);
         le.ID__c = U_Id;
         update le;
        } 
        catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug(res.toString());
            res.getbody();
               }
            }
       }

 
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest
private class ApexCalloutTest{
    private static testMethod void doTest() {

		Lead led = new Lead();
		led.Lastname ='Test';
		led.Email ='test@test.com';
		led.company ='test';
		insert led;
		
		List<Lead> lstLead = new List<Lead>();
		lstLead.add(led);
		
        Test.startTest();

			ApexCallout.invokecallout(lstLead);
        Test.stopTest();

    }
}

Let us know if this will help you
parth jollyparth jolly
Hi
Put the call to the future method inside startTest/stopTest:
Test.startTest();

myClass.futuremethod( someID );

Test.stopTest();

Test.stopTest() does not return until your future method has completed.
 
Your other problem will be that you can't make an actual web callout while testing.  Use "Test.isRunningTest()" to determine whether to make the callout:
if ( !Test.isRunningTest() ) new Http().send(request);


I wrote it this way because in your example, you don't use the HttpResponse that's returned...
 
If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!
 
Aditya Singh 37Aditya Singh 37
@amit chaudhary: This is giving error 
Methods defined as TestMethod do not support Web service callouts