+ Start a Discussion
Aditya Singh 37Aditya Singh 37 

Apex Test Class for Future callOut

 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
public static void invokecallout(list<Lead> Leads) {
WS_Webservice.Notification(Leads[0].id, Leads[0].name)
private class ApexCalloutTest{
    private static testMethod void doTest() {

        Lead l = new Lead(id = '00XXXXXXXXXXXXXX',
                          name = 'User123');


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();

        //Setting Method Content and Body
        'name='+EncodingUtil.urlEncode(name, 'UTF-8')
        try {
         res = http.send(req);
         Dom.Document docx = new Dom.Document();
         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);

Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
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>();



Let us know if this will help you
parth jollyparth jolly
Put the call to the future method inside startTest/stopTest:

myClass.futuremethod( someID );


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