[SalesForce] call HTTpRequest in test class

public with sharing class uu{
static c constant =c.getInstance('chjstant');

public static HTTpRequest genequest( String EndPointUrl,String Method,Map<String,String>headerMap,String body ){
    HttpRequest req = new HttpRequest();
           return req;
}


public  Http h = new Http();
    HttpResponse res = new HttpResponse();
    if(EndPointUrl != null)
    {
    res = h.send( TReqst( End_Point_Url,Mehod,headep,bdy ) );
    }
    return re;
}
}

Best Answer

You can create a test response by three implementation- create a mock response class, mock json response from static resorces ,Performing DML Operations and Mock Callouts. Please see the devlopment guide how to test a http request. Link- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing.htm

Example Code For testing-

Mock Http Response class-

@isTest
global class MockHttpGoogleApiResponseGenerator implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
        // Optionally, only send a mock response for a specific endpoint
        // and method.

    System.assertEquals('GET', req.getMethod());

    // Create a fake response
   HttpResponse res = new HttpResponse();
   res.setBody('{"location" :{"lat" : 36.0902807,"lng" : -79.43770619999999}}');

        res.setStatusCode(200);

    return res;
}
}

Test Class -

@isTest(SeeAllData=true)
private class TestAccountGoogleApiHelper{

@isTest static void TestAccountWithGeocordinate() {
    // Test data setup
    // Create an account with an opportunity, and then try to delete it
   Test.setMock(HttpCalloutMock.class, new MockHttpGoogleApiResponseGenerator());
    Account acct = new Account(Name='Burlington Textiles Corp of America (sample)');
    acct.BillingStreet='535 S. Lexington Ave';
    acct.BillingCity='Burlington';
    acct.BillingState='NC';
    acct.BillingPostalCode='27215';
    acct.BillingCountry='USA'; 
    // Perform test

    insert acct;

    // Verify 
    // In this case the deletion should have been stopped by the trigger,
    // so verify that we got back an error.
     acct=[SELECT Id,Name,Location__c, Location__Latitude__s, Location__Longitude__s FROM Account where Name ='Burlington Textiles Corp of America (sample)'];
     System.assertEquals(acct.Name,'Burlington Textiles Corp of America (sample)');

     acct.BillingStreet='560 S. Lexington Ave';



     update acct;

     acct=[SELECT Id,Name,BillingStreet,Location__c, Location__Latitude__s, Location__Longitude__s FROM Account where Name ='Burlington Textiles Corp of America (sample)'];
     System.assertEquals(acct.BillingStreet,'560 S. Lexington Ave');
   //  System.assertEquals(String.valueof(acct.Location__Latitude__s),'-79.43787499999999');

}

}

As you can see in The test class has statement to set mock response and when a account is insert a trigger that fires a http callout to Google Api get this mock response define in mock test class.

Related Topic