I have a future method which sends out two callouts. It looks something like this
@future(callout = true)
static webservice void SendCRMLeadFromTrigger(string leadID){
//FIRST CALLOUT
HTTP httpInstance = new HTTP();
HTTPRequest request = new HTTPRequest();
request.setEndpoint(endpointURL);
request.setHeader('Content-Type','text/xml');
request.setHeader('SOAPAction','xxxxxxxxxxxxx/GetSecurityTokenOperation');
request.setMethod('POST');
request.setBody(body);
string responseBody;
try{
HTTPResponse newResponse = httpInstance.send(request);
responseBody = newResponse.getBody();
}catch(exception e){}
//SECOND CALLOUT
HTTP httpInstance2 = new HTTP();
HTTPRequest request2 = new HTTPRequest();
request.setEndpoint(endpointURL2);
request.setHeader('Content-Type','application/soap+xml');
request.setHeader('SOAPAction','xxxxxxxxxxxxx/createLead');
request.setMethod('POST');
request.setBody(body2);
string responseBody2;
try{
HTTPResponse newResponse2 = httpInstance2.send(request2);
responseBody2 = newResponse2.getBody();
}catch(exception e){}
}
I order to test this method, I created an class which is implementation of MockHttpResponseGenerator class. The class look something like this
public class MockHttpResponseGenerator implements HttpCalloutMock
{
public HTTPResponse respond(HTTPRequest req)
{
HttpResponse res = new HttpResponse();
if(req.getBody().contains('salesforce43')){
String response='RESPONSE 1';
res.setHeader('Content-Type', 'text/xml');
res.setBody(response);
}else if(req.getBody().contains('<cre:CreateLeadInput>')){
String response='RESPONSE 2';
res.setHeader('Content-Type', 'application/soap+xml');
res.setBody(response);
}
res.setStatusCode(200);
return res;
}
}
I am calling this class in my test method
public static testmethod void testMultiMockResponse()
{
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
//create lead
Lead thisLead = new Lead();
insert thisLead;
string leadID = thisLead.Id;
CalloutClass.SendCRMLeadFromTrigger(leadID);
}
However my test method is not able to cover the response part as there is no HTTP response is null. I am wondering if this is the correct way to test multiple callouts within a method.
In my MockHttpResponseGenerator implementation, I am returning the response based on unique keywords present in each SOAP call.