I have written test class for my http future method I am receiving assertion error. Please look at my class & test class below.

public with sharing class contactAutoAddress {
private static boolean isTrigger = false;
public class contactException extends Exception {}
public static void onAfterInsert(List<Contact> triggerNew)
    if(isTrigger == true)
        isTrigger = true;
        List<Id> contactsId = new List<Id>();

        for(Contact c : triggerNew)
        system.debug(LoggingLevel.Info, 'onAfterInsert called with '+triggerNew+'; sending IDs='+contactsId);
@future(callout = true)
public static void makeUSPSCallout(List<Id>cnt)
    string resp;
    List<Contact> contactsSet = [SELECT Id, MailingPostalCode, MailingCity, MailingState, MailingCountry FROM contact where id = :cnt];

    for(Contact c : contactsSet)
        String ZipCode = c.MailingPostalCode;
        String userID = '************';
        String reqXML = '<CityStateLookupRequest USERID="'+userID+'"><ZipCode ID= "0"><Zip5>'+zipCode+'</Zip5></ZipCode></CityStateLookupRequest>';
        string endpoint ='http://production.shippingapis.com/ShippingAPITest.dll?API=CityStateLookup&XML=';
        endpoint = endpoint+EncodingUtil.urlEncode(reqXML,'UTF-8');
        system.debug(LoggingLevel.Info,'zipCode.cls: calling endpoint='+endpoint);
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        HttpResponse res = new HttpResponse();
        req.setHeader('Content-Type', 'application/xml');

        try {
            res = http.send(req);
            if (res.getStatusCode() != 200) {
                throw new contactException(res.getStatus());
        } catch (contactException e) {
            system.debug(LoggingLevel.Error, 'Error HTTP response code = '+res.getStatusCode()+'; calling '+endpoint );
        resp = res.getBody();
        system.debug('res :' + resp); 
        Dom.Document docx = new Dom.Document();
        system.debug('Docx' + docx);
        Dom.XmlNode xroot = docx.getRootElement();
        Dom.XmlNode zipNode = xroot.getChildElement('ZipCode',null),
            stateNode = zipNode == null? null: zipNode.getChildElement('State',null),
                cityNode = zipNode == null? null: zipNode.getChildElement('City',null);

        if(xroot.getChildElement('ZipCode',null)!= null){

            if(stateNode != null && stateNode.getText() != null){
                String state = xroot.getChildElement('ZipCode',null).getChildElement('State',null).getText();
                c.MailingState = state;
                system.debug('State'+ state);
            if(cityNode != null && cityNode.getText() != null)
                string city = xroot.getChildElement('ZipCode',null).getChildElement('City',null).getText();
                string titleCaseCity = properCase.toTitleCase(city);
                c.MailingCountry = 'US';
                c.MailingCity = titleCaseCity;
    update contactsSet;     
    isTrigger = false;  

My test class

static testmethod void testMe(){
        contact cnt = new contact(lastName = 'test', email='test@test.com');        
        cnt.MailingPostalCode = '12345';
        cnt.MailingState = 'NY';
        cnt.MailingCity = 'New York';
        cnt.MailingCountry= 'US';
        insert cnt;
        Test.setMock(HttpCalloutMock.class, new mockAddress());
        contactAutoAddress.makeUSPSCallout(new List<Id> {cnt.Id} );
        //HttpResponse response = new HttpResponse();
        //String contentType = response.getHeader('Content-Type');
        //system.assertEquals(contentType, 'application/xml');
        //String actualValue = response.getBody();
        //String expectedValue = '{"foo":"bar"}';
        //System.assertEquals(actualValue, expectedValue);
        contact c = [select id,name,MailingAddress from Contact where id=:cnt.Id];


Mock test class

global class mockAddress implements HttpcalloutMock {
global HTTPResponse respond(HTTPRequest req) {

        System.assertEquals('http://api.salesforce.com/foo/bar', req.getEndpoint());
        System.assertEquals('GET', req.getMethod());
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/xml');
        res.setBody('<CityStateLookupResponse> <ZipCode ID="0"> <Zip5>90210</Zip5> <City>BEVERLY HILLS</City> <State>CA</State> </ZipCode> <ZipCode ID="1"> <Zip5>20770</Zip5> <City>GREENBELT</City> <State>MD</State> </ZipCode> </CityStateLookupResponse>');
        return res;

error I am getting

System.AssertException: Assertion Failed: Expected: http://api.salesforce.com/foo/bar, Actual: http://production.shippingapis.com/ShippingAPITest.dll?API=CityStateLookup&XML=%3CCityStateLookupRequest+USERID%3D%22306LIBER4330%22%3E%3CZipCode+ID%3D+%220%22%3E%3CZip5%3E12345%3C%2FZip5%3E%3C%2FZipCode%3E%3C%2FCityStateLookupRequest%3E

    string endpoint ='http://production.shippingapis.com/ShippingAPITest.dll?API=CityStateLookup&XML=';




    System.assertEquals('http://api.salesforce.com/foo/bar', req.getEndpoint());

Your mock class is expecting one URL, but your code provides another. You will probably want to either remove that assert (mock classes typically don't contain assertions), or change it to match your code.

  • THank you so much. I removed the assert from the mock and was able to execute my test class successfully. My code coverage is also over 90% with this test method. Thanks a ton...
    – Aman
    May 12 '17 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.