I am attempting to write a series of unit tests on a 2300+ line APEX class. The gist of the class is that it makes a Rest Request and retrieves an XML object from a web service. The class then assigns nodes from the XML file to objects in the Account, Opportunity and other object tables.
At this time, we have 0% code coverage. What should be tested, what should not be tested (such as the web service call), and how can we assure satisfactory code coverage for this type of class?
After seeing this question put on hold as too broad, and working a bit with REST request and HTTP Callout mocks, I've come up with the following:
public with sharing class CalloutClass {
public static HttpResponse getInfoFromExternalService(){
RestRequest req = new RestRequest();
req.requestURI = URL.getSalesforceBaseUrl().toExternalForm() + '/services/apexrest/IFXLoadXML/TEST';
req.setMethod ('POST');
Http h = new Http();
RestResponse res = h.send(req);
return res;
}
}
The test now has an error stating that HttpRequest doesn't have requestURI. So, I need to essentially make this mock a Rest request instead of an HttpRequest. Is that correct? If so, please help with any example code available to accomplish this callout mock within a test class. Thanks, and apologies for the broad initial question.
Best Answer
Your logic should be tested completely... for webservice calls there are mock callouts you can make to test out your logic. Salesforce HttpCalloutMock Doc
Pretty much your mock method will have your preloaded response since test cases cannot make callouts, this way your apex logic can run as though it is making a callout.
Your http mock callout will look like such.
and your test classes will know what callout to use by providing it with the class
Working example provided by salesforce within the doc is as follows:
HTTP CALLOUT MOCK
ORIGINAL CALLOUT METHOD
TEST CASE