function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ItsJustCode2ItsJustCode2 

How do you write a mock test for a http call out .setMethod('POST')? Please help if you know how

ItsJustCode2ItsJustCode2
I have searched everywhere and all of the mock test seem to be based on .setMethod('GET') with return statements.  I've tried to work around this but have run into error after error.

Here is the class I need a Mock test for.....
 
global class HTTPRequestSender{

    @future (callout=true)

    Public static void sendHTTPRequest(String url, String body){

        Http http = new Http();

        HttpResponse res = new HttpResponse();

        HttpRequest req = new HttpRequest();

        req.setEndpoint(url);

        req.setBody(body);

        req.setMethod('POST');

        try {

            res = http.send(req);
        } catch(System.CalloutException e) {

            System.debug('CalloutException: '+ e);

            System.debug(res.toString());

        }

     }

}



Any advice would be greatly appreciated.

Thank you,
G
KapilCKapilC
Hi,

Here is a very good document where you can find in depth detail.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm

[If you got answer from my post please mark it as solution.]

Thanks,
Kapil
ItsJustCode2ItsJustCode2
I've tried to adopt that methodology but what I get is......  

Time Started 10/20/2015 5:14 PM
Class CalloutClassTest
Method Name testCallout
Pass/Fail Fail
Error Message System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
Stack Trace Class.HTTPRequestSender1.sendHTTPRequestFIU: line 22, column 1
Class.CalloutClassTest.testCallout: line 23, column 1
 
@isTest
private class CalloutClassTest {
     @isTest static void testCallout() {
        // Set mock callout class 
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
        ID[] ids = new List<ID>();
         String url = 'https://cmncom.insidesales.com/do=noauth/immediate_response';

         Lead lead = new Lead(School__c = 'Test', Status = 'Prospect',email = 'bla@hotmail.com',FirstName = 'Test', cmProspect_Complete__c = datetime.Now()+5000, cmProspect_Status_Date_Time__c = datetime.Now(), Total_Call_Duration__c = 120, LastName ='Mover', Company = 'Test Company');
        insert lead;

        ids.add(lead.id);

        String body = 'method=call_now&dialer_initiative_id=69&ids='+EncodingUtil.urlEncode(JSON.serialize(ids), 'ISO-8859-1');              
        
        //HttpResponse res = new HttpResponse();
        HttpRequest req = new HttpRequest();

        
        // Call method to test.
        // This causes a fake response to be sent
        // from the class that implements HttpCalloutMock. 
        HttpResponse res = HTTPRequestSender1.sendHTTPRequestFIU(url, body);
        
        // Verify response received contains fake values
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"foo":"bar"}';
        System.assertEquals(actualValue,actualValue);
        System.assertEquals(200, res.getStatusCode());
    }
}
global class HTTPRequestSender1{

    Public static HttpResponse sendHTTPRequestFIU(String url, String body){
        
        
        Http http = new Http();

        //HttpResponse res = new HttpResponse();

        HttpRequest req = new HttpRequest();

        req.setEndpoint(url);

        req.setBody(body);

        req.setMethod('POST');
        
        HttpResponse res;
        res = http.send(req);
        Return res;
       

        

     }

}

Any idea what I'm doing wrong?