Find your content:

Search form

You are here

Writing a test class for multi-callout method using MockHttpResponseGenerator

 
Share

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.


Attribution to: Yash Mehta

Possible Suggestion/Solution #1

Your approach looks basically OK: your mock should generate different responses based on the request data. The code would be clearer and reflect the ideas of SOAP a bit more if you based the conditional logic on the SOAPAction header instead of more obscure values in the request bodies.

But exactly what is wrong is hard to tell from your comments: I suggest you add System.debug calls to all 3 classes and check the values step by step.


Attribution to: Keith C
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33263

My Block Status

My Block Content