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
Rakesh ImsaniRakesh Imsani 

HOw to write Test class for @future Method

From trigger i am calling this following class/method, how can write Test class.


global class FutureClass{

 @Future (callout=true)
    public static void SendCaseData(String sCaseNo,String sCaseEmail, String sCaseContact)
    {
       
//WSDL2Apex class     
 SF2ERPCase.SFSendSoap sfsend = new F2ERPCase.SFSendSoap();
          
        String parmXML;
        
        parmXML = '<Root>';
        parmXML += '<CaseNo>' + sCaseNo + '</CaseNo>';
        parmXML += '<CaseEmail>' + sCaseEmail + '</CaseEmail>';
        parmXML += '<CaseContact>' + sCaseContact  + '</CaseContact>';
        parmXML += '</Root>';
        
        sfsend.RecvMsg(parmXML);
       }
}
Hitendar Singh 9Hitendar Singh 9
Hi

Please go through the below link. It will help.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm
Ravi Dutt SharmaRavi Dutt Sharma
Use Test.startTest(), then call your future method and then call Test.stopTest(). 
Start test and Stop test methods will convert your async call into sync call.
Rakesh ImsaniRakesh Imsani
Hi Ravi, I tried as you mentioned but i am facing " Methods defined as TestMethod do not support Web service callouts, test skipped" error. What to do ?
Hitendar Singh 9Hitendar Singh 9
Hi Rakesh

Before making a callout just check whether Test class is running and send a sample response if method is called from test class. Like below


if (Test.isRunningTest()) {   
      //Make the callout here
}
else {  
 ..   response = 'Sample response';
}

Hope this helps
Rakesh ImsaniRakesh Imsani
@isTest
Public class TestFutureClass 
{

    static testMethod void  testSendCaseData()
    {
        Test.startTest();
    //Inserted Case obj data

              FutureClass.SendCaseData('00017446','rimsani@tyco.comm','rockey');
              
        Test.stopTest();
    
    }
    
}
Rakesh ImsaniRakesh Imsani
Hi Hitendar,

Where can i write the code?  In Test class or Original class??

Above is my test class
Hitendar Singh 9Hitendar Singh 9
Hi 

You cannot do a callout in text class execution. Instead we have to bypass the callout like below.


if (Test.isRunningTest()) {   
      FutureClass.SendCaseData('00017446','rimsani@tyco.comm','rockey');
}
 
Hitendar Singh 9Hitendar Singh 9
global class FutureClass{

 @Future (callout=true)
    public static void SendCaseData(String sCaseNo,String sCaseEmail, String sCaseContact)
    {
       
//WSDL2Apex class     
 SF2ERPCase.SFSendSoap sfsend = new F2ERPCase.SFSendSoap();
          
        String parmXML;
        
        parmXML = '<Root>';
        parmXML += '<CaseNo>' + sCaseNo + '</CaseNo>';
        parmXML += '<CaseEmail>' + sCaseEmail + '</CaseEmail>';
        parmXML += '<CaseContact>' + sCaseContact  + '</CaseContact>';
        parmXML += '</Root>';
        
if (Test.isRunningTest()) {   
sfsend.RecvMsg(parmXML);
       }
}
Hitendar Singh 9Hitendar Singh 9
modify your original class like above
Rakesh ImsaniRakesh Imsani
Hi Hitendar,

I made changes like you suggested but NO Luck ???