You need to sign in to do that
Don't have an account?

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);
}
}
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);
}
}
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
Start test and Stop test methods will convert your async call into sync call.
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
Public class TestFutureClass
{
static testMethod void testSendCaseData()
{
Test.startTest();
//Inserted Case obj data
FutureClass.SendCaseData('00017446','rimsani@tyco.comm','rockey');
Test.stopTest();
}
}
Where can i write the code? In Test class or Original class??
Above is my test class
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');
}
@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);
}
}
I made changes like you suggested but NO Luck ???