You need to sign in to do that
Don't have an account?
Aditya 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();
}
}
}
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();
}
}
}
Let us know if this will help you
Put the call to the future method inside startTest/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:
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!
Methods defined as TestMethod do not support Web service callouts