+ Start a Discussion
Sean O-ConnellSean O-Connell 

help with test for HTTP Call Out

I have made a trigger and a class for when a contact gets created it sends Contact information to a third party.  So when I run my normal contact test the trigger gets 100% coverage but the Class gets skipped and I get this error 'Methods defined as TestMethod do not support Web service callouts, test skipped'

How do I create a test for this?

Class
public class sendHttpPostToCV{

@future(callout=true)
    public static void sendHttpRequest(String url){

        Http h = new Http();
        
        HttpRequest req = new HttpRequest();
        req.SetEndPoint(url);
        req.setMethod('POST');
        req.setHeader('Content-Type', 'text/html');
        req.setBody(''); 
        //req.setTimeout(60000);
        
        HttpResponse res = h.send(req);
        
    
        
    }
}

Trigger

trigger HttpPostToCV on Contact (after insert) {
    List<String> leadIds = new List<String>();
    
    for (Contact c : Trigger.new){
    
        String LeadSource = c.LeadSource;
        String FirstName = c.FirstName;
        String LastName = c.LastName;
        String MailingStreet = c.MailingStreet; 
        String MailingCity = c.MailingCity;
        String MailingState =  c.MailingState;
        String MailingPostalCode = c.MailingPostalCode;
        String MailingCountry = c.MailingCountry;
        String Email = c.Email;
        String Phone = c.Phone;
        String OtherPhone = c.OtherPhone;
        String Program_Code = c.Program_Code__c;
        String Previous_Education = c.Previous_Education__c;
        String Affiliation_Code = c.Affiliation_Code__c;
        String SalesForceID = c.id;
        
        String url = 'http://eleads.lafilm.com:8088/Cmc.Integration.LeadImport.HttpPost/ImportLeadProcessor.aspx?Format=CollegeDirectories&leadsource='
               + LeadSource + '&Firstname=' + FirstName + '&Lastname=' + LastName + '&Address1=' + MailingStreet + '&Address2=&City='
               + MailingCity + '&State=' + MailingState + '&Postalcodeorzip=' + MailingPostalCode + '&Country='
               + MailingCountry + '&Email=' + Email + '&Phone=' + Phone + '&WorkPhone=' + OtherPhone + '&Program='
               + Program_Code + '&Campus=MAIN&PreviousEducation=' + Previous_Education + '&Agency='
               + Affiliation_Code + '&ZSalesForceID=' + SalesForceID;
        
    
        sendHttpPostToCV.sendHttpRequest(url);
    }
}

Current Contact object test 

@isTest
public class TestContactTriggers {
    static testmethod void test()
    {
        
        
        Account acc = New Account(Name = 'Test Account');
        insert acc;
        
        Contact con = New contact ();
        con.FirstName = 'test';
        con.LastName = 'con';
        con.AccountId = acc.Id;
        con.Event_Scheduled_Date__c = system.now();
        con.Lead_Source_Code__c = 'test';
        con.school_status__c = 'Connected';
        insert con;
        
        Contact con2 = New contact ();
        con2.FirstName = 'test';
        con2.LastName = 'con';
        insert con2;
        
        
        datetime eventscheduleddate = con.Event_Scheduled_Date__c;
        datetime newdate = eventscheduleddate.addHours(1);
        
        Test.startTest();
        
        
        
        con.Event_Scheduled_Date__c = newdate;
        con.Lead_Source_Code__c = 'test2';
        con.school_status__c = 'test';
        Update con;
        
        con.school_status__c = 'connected';
        update con;   
        
        
        Test.stopTest();
        
    }
}

Thanks

Sean
Best Answer chosen by Sean O-Connell
Pankaj MehraPankaj Mehra
Hi Sean,

You need to create a HTTP Mock class that implement HTTPCalloutMock interface where you will create a test response. Once the mock is created you need to add the created Mock class in test class

Test.setMock(HttpCalloutMock.class, new HttpMockCls());
            
Here is a sample Mock class:

@isTest
public class HttpMockCls implements HttpCalloutMock {
        protected Integer code;
        protected String status;
        protected String bodyAsString;
        protected Blob bodyAsBlob;
        protected Map<String, String> responseHeaders;

        public HttpMockCls(Integer code, String status, String body,
                                         Map<String, String> responseHeaders) {
            this.code = code;
            this.status = status;
            this.bodyAsString = body;
            this.bodyAsBlob = null;
            this.responseHeaders = responseHeaders;
        }

        public HttpMockCls(Integer code, String status, Blob body,
                                         Map<String, String> responseHeaders) {
            this.code = code;
            this.status = status;
            this.bodyAsBlob = body;
            this.bodyAsString = null;
            this.responseHeaders = responseHeaders;
        }

        public HTTPResponse respond(HTTPRequest req) {
            HttpResponse resp = new HttpResponse();
            resp.setStatusCode(code);
            resp.setStatus(status);
            if (bodyAsBlob != null) {
                resp.setBodyAsBlob(bodyAsBlob);
            } else {
                resp.setBody(bodyAsString);
            }

            if (responseHeaders != null) {
                 for (String key : responseHeaders.keySet()) {
                resp.setHeader(key, responseHeaders.get(key));
                 }
            }
            return resp;
        }
}

Thanks