You need to sign in to do that
Don't have an account?
Valentin F.
Unable to test callout into future method
Hello everybody,
I need to test a callout situated in a @future method.
I used the 'Test.setMock(HttpCalloutMock.class, new AppelAPIMock());' to do it but I am not able to test XmlNode.
Here is an example to make it easier for you to understand :
@future(callout=true) ... //Method realizing the callout HttpResponse resp = appelDpd.getTrackingDPD(xmlBody); String responseBody = resp.getBody(); if(resp.getStatusCode() == 200) { Dom.Document xmlD = new Dom.Document(); xmlD.load(responseBody); //All this part is not supported by tests method Dom.XmlNode root = xmlD.getRootElement(); Dom.XmlNode body = root.getChildElement('Body',null); Dom.XmlNode trace = body.getChildElement('getShipmentTraceResponse', null); Dom.XmlNode traceResult = trace.getChildElement('getShipmentTraceResult', null); for(Dom.XmlNode node : traceResult.getChildElements()) { if(node.getName() == 'ShippingDate' && node != null && node.getText().trim() != ''){}
I finished xml string by '</soap:Body>' and '</soap:Envelope>' and not '</soap12:Body>' and '</soap12:Envelope>'.
Stupid error ...
But it stil not going into if/else, essentially when I check for statusNumber (here even the 308 statusNumber is not covered :
All Answers
Have you surrounded the callout for the future method in test class with Test.startTest() and Test.stopTest()? And does your AppelAPIMock set valid response body and response code as 200?
Regards,
MKR
I did in my test class : AppelAPIMock.apxc I wrote the same nodes as expected in my class.
I finished xml string by '</soap:Body>' and '</soap:Envelope>' and not '</soap12:Body>' and '</soap12:Envelope>'.
Stupid error ...
But it stil not going into if/else, essentially when I check for statusNumber (here even the 308 statusNumber is not covered :
For the test class of future callout method, you have to create a callout Mock class which implements the HttpCalloutMock, because Methods defined as TestMethod do not support Web service callouts and you are doing Web service callouts, so you have to create a fake callout response.
for creating a HttpCalloutMock class please go through the given link as it may help you in solving your problem.
-https://www.thephani.com/test-classes-for-future-methods-callout-true/
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
Please ensure to include namespace in the getChildElement calls like this: Regards,
MKR